用lambda加入两个列表

时间:2014-01-31 00:10:50

标签: c# list join lambda

我有两个列表,我需要加入或创建一个新列表,将第二个列表中的一个值添加到列表具有相同值的第一个列表

清单1

Order| Material |TotalQuantity |Desc
--------------------------------
    1| M1       | 100          |text here
    2| M3       | 20           |text here
    3| M1       | 30           |text here
    1| M5       | 50           |text here

清单2

Order| Material |QuantitySell
--------------------------------
    1| M1       | 10

结果

Order| Material |TotalQuantity |Desc      |QuantitySell
-------------------------------------------------------
    1| M1       | 100          |text here |10
    2| M3       | 20           |text here |
    3| M1       | 30           |text here |
    1| M5       | 50           |text here |

我有这段代码:

finalList.AddRange(
    list1.Distinct()
    .join(
    list2.Select(x=>x).Distinct(),                    
    l1 => new { material = l1.parte.ToUpper().Trim(), Order= l1.orden.ToUpper().Trim()},
    l2=> new {material = l2.parte.ToUpper().Trim(), Order=l2.Order.ToUpper().Trim()},                    
    (l1,l2) => 
    new ListSell
    {
      TotalQuantity = l1.TotalQuantity ,
      QuantitySell= l2.QuantitySell,
      Desc= l1.Desc,
      Material = l1.Material ,
      Orden = l1.orden
    }
    ).ToList()
    );

---- ---编辑

我有这个错误

  

错误11' System.Collections.Generic.IEnumerable'不包含' join'的定义没有延伸方法加入'接受类型为'System.Collections.Generic.IEnumerable'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)

想知道如何合并两个列表?

4 个答案:

答案 0 :(得分:1)

这是lambda版本

 var list1 = new List<OrderProduct>()
            {
                new OrderProduct(1, "M1", 100, "Text Here"),
                new OrderProduct(2, "M3", 20, "Text Here"),
                new OrderProduct(3, "M1", 30, "Text Here"),
                new OrderProduct(4, "M5", 50, "Text Here"),
            };
        var list2 = new List<OrderSold>()
            {
                new OrderSold(1, "M1", 10),
            };


 var result = list1.GroupJoin(
                list2,
                product => new { product.Order, product.Material },
                sold => new { sold.Order, sold.Material},
            (p, g) => g
        .Select(c => new ListSell
            {
                Order = p.Order,
                Material = p.Material,
                TotalQuantity = p.TotalQuantity,
                Description = p.Description,
                QuantitySell = c.QuantitySell
            })
        .DefaultIfEmpty(new ListSell
            {
                Order = p.Order,
                Material = p.Material,
                TotalQuantity = p.TotalQuantity,
                Description = p.Description,
                QuantitySell = 0
            }))
        .SelectMany(g => g);
    }

答案 1 :(得分:1)

您正在重复使用resultSelctor中的l1和l2引用(连接方法的最后一个参数)

将其更改为:

        var query = list1.Join(
            list2,
            l1 => new { l1.Order, l1.Material },
            l2 => new { l2.Order, l2.Material },
            (item1, item2) => new
            {
                TotalQuantity = item1.TotalQty,
                QuantitySell = item2.TotalQty,
                Desc = item1.Desc,
                Material = item1.Material,
                Orden = item1.Order
            });

答案 2 :(得分:1)

这是“加入”而不是“加入”。这是区分大小写的。

答案 3 :(得分:0)

你需要左外连接:

finalList.AddRange(
    (from l1 in list1
     join l2t in list2 on 
         new { material = l1.parte.ToUpper().Trim(), Order = l1.orden.ToUpper().Trim() }
     equals
         new { material = l2.parte.ToUpper().Trim(), Order = l2.Order.ToUpper().Trim() }
     into list2Joined
     from l2 in list2Joined.DefaultIfEmpty()
     select new  ListSell
     {
         TotalQuantity = l1.TotalQuantity ,
         QuantitySell= l2.QuantitySell,
         Desc= l1.Desc,
         Material = l1.Material ,
         Orden = l1.orden
     }).ToList());