我有2個表像下:合併兩個列表成爲一個
var selectedItems = _uow.ProductItems.Get(w => w.ProductId == productid).Select(projection => new
{
ProductId = projection.ProductId,
ProductItemId = projection.ProductItemId,
ProductItemTypeId = projection.ProductItemTypeId,
ProductItemName = GetItemName(projection),
ProductItemType = projection.ItemType,
Amount = projection.Amount
});
var services = _uow.Services.GetAll().Select(projection => new {
ProductId = 0,
ProductItemId = 0,
ProductItemTypeId = projection.ServiceId,
ProductItemName = projection.Name,
ProductItemType = "Service",
Amount = projection.DefaultAmount
});
我希望能夠將它們合併到使用我的自定義邏輯使用Linq
一個列表,它是保持只有ProductItemTypeId
對象匹配ProductId
或ProductItemId
是不是0
。我已經實現了這一點,但使用foreach
象下面這樣:
List<dynamic> productItems = new List<dynamic>();
foreach (var item in services)
{
if (item.ProductItemTypeId == selectedItems.Select(s => s.ProductItemTypeId).FirstOrDefault())
{
productItems.Add(selectedItems.Where(w => w.ProductItemTypeId == item.ProductItemTypeId).FirstOrDefault());
}
else
{
productItems.Add(item);
}
}
我會很感激,如果任何人都可以建議我怎麼能在Linq
寫上面的邏輯,這樣我的代碼更簡潔。
只是在黑暗中拍攝,也許你可以試試[聯盟](http://msdn.microsoft.com/en-us/library/bb341731%28v=vs.110%29.aspx) – jacqijvv
檢查這出:http://stackoverflow.com/questions/21951459/merge-two-or-more-list-according-to-order/21951484#21951484 –
@Noobacode感謝您的評論,但請參閱我的評論下面的'郵編' – lbrahim