Merge Two Lists Into One

痴心易碎 提交于 2019-12-25 10:22:09

问题


I have 2 list like following:

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. I have achieved this but using foreach like below:

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.


回答1:


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:

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



回答2:


You can use Zip with 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

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


来源:https://stackoverflow.com/questions/22087845/merge-two-lists-into-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!