2014-02-28 45 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

我会编辑我的答案;) –