【问题标题】:Merge Two Lists Into One将两个列表合并为一个
【发布时间】:2014-04-01 00:33:37
【问题描述】:

我有 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 匹配其中ProductIdProductItemIdNOT0。我已经实现了这一点,但使用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 中编写上述逻辑,我将不胜感激,这样我的代码会更简洁。

【问题讨论】:

标签: c# linq list dynamic


【解决方案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));

【讨论】:

  • 感谢您的回答。但是如果数字不匹配,Zip 会将对象留在列表中。我尝试合并的列表中的对象数量可能不相等。
【解决方案2】:

据此:

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

【讨论】:

    猜你喜欢
    • 2014-01-27
    • 2015-04-01
    • 2021-04-04
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多