Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join two list with lambda

Tags:

c#

join

list

lambda

I have two list and I need join or create a new list adding one value from the second list to the first list where the list has the same values

List 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

List 2

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

result

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 |

I have this code:

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

----Edit---

I have this error

Error 11 'System.Collections.Generic.IEnumerable' does not contain a definition for 'join' and no extension method 'join' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

would like to know how to combine the two lists?

like image 851
miguelSnam Avatar asked Aug 25 '26 03:08

miguelSnam


1 Answers

Here is the lambda version

 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);
    }
like image 97
Lawrence Thurman Avatar answered Aug 26 '26 16:08

Lawrence Thurman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!