lbrahim
lbrahim

Reputation: 3810

Merge Two Lists Into One

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.

Upvotes: 0

Views: 1118

Answers (2)

Oscar Bralo
Oscar Bralo

Reputation: 1907

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

Upvotes: 1

Amir Sherafatian
Amir Sherafatian

Reputation: 2083

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

Upvotes: 0

Related Questions