简体   繁体   English

将两个列表合并为一个

[英]Merge Two Lists Into One

I have 2 list like following: 我有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
});

I want to be able to merge them into a single list using Linq using my custom logic which would be to keep only the objects if ProductItemTypeId matches where ProductId or ProductItemId is NOT 0 . 我希望能够使用Linq使用我的自定义逻辑将它们合并到一个列表中,如果ProductItemTypeId匹配,其中ProductIdProductItemId 不为 0则仅保留对象。 I have achieved this but using foreach like below: 我已经实现了,但是使用如下所示的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);
    }
}

I would really appreciate if anyone can suggest how I can write the above logic in Linq so that my code is more concise. 如果有人可以建议我如何用Linq编写上述逻辑,以便使我的代码更简洁,我将不胜感激。

You can use Zip with Linq. 您可以将Zip与Linq一起使用。

Enumerable.Zip<TFirst, TSecond, TResult>

This will generate a new list of the union of two. 这将生成一个新的二合一列表。

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

I hope this helps 我希望这有帮助

EDIT: Try something like this: 编辑:尝试这样的事情:

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

according to this: 根据这个:

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

you can use left join , instead: 您可以使用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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM