2014-02-28 53 views
0

我有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對象匹配ProductIdProductItemId不是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寫上面的邏輯,這樣我的代碼更簡潔。

+5

只是在黑暗中拍攝,也許你可以試試[聯盟](http://msdn.microsoft.com/en-us/library/bb341731%28v=vs.110%29.aspx) – jacqijvv

+0

檢查這出:http://stackoverflow.com/questions/21951459/merge-two-or-more-list-according-to-order/21951484#21951484 –

+0

@Noobacode感謝您的評論,但請參閱我的評論下面的'郵編' – lbrahim

回答

0

:嘗試是這樣的

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); 
    } 
} 

可以使用left join,而是:

var query = (from service in services 
      join item in selectedItems 
       on service.ProductItemTypeId equals item.ProductItemTypeId 
       into joinedList 
      from item in joinedList.DefaultIfEmpty() 
      select new 
      { 
       ProductId = item != null ? item.ProductId : service.ProductId, 
       ProductItemId = item != null ? item.ProductItemId : service.ProductItemId, 
       ProductItemTypeId = item != null ? item.ProductItemTypeId : service.ProductItemTypeId, 
       ProductItemName = item != null ? item.ProductItemName : service.ProductItemName, 
       ProductItemType = item != null ? item.ProductItemType : service.ProductItemType, 
       Amount = item != null ? item.Amount : service.Amount 
      }) 
      .ToList(); 
1

您可以使用Zip與Linq。

Enumerable.Zip<TFirst, TSecond, TResult> 

這將生成兩個聯合的新列表。

http://msdn.microsoft.com/es-es/library/dd267698(v=vs.110).aspx

我希望這有助於

編輯:根據本

var listResult = list1.Where(x => x.ProductId == 0 || x.ProductItemId == 0).Concat(list2.Where(x => x.ProductId == 0 || x.ProductItemId == 0)); 
+0

感謝您的回答。但如果數字不匹配,'Zip'會將列表中的對象留在列表中。我試圖合併的列表可能有不等數量的對象。 – lbrahim

+0

我會編輯我的答案;) –