none
How to update my list1 property value by list2 property value by joining RRS feed

  • Question

  • var list1 = new List<ItemOne>
                            {
                                new ItemOne {IDItem = 1, OneProperty = "1"},
                                new ItemOne {IDItem = 2, OneProperty = null},
                                new ItemOne {IDItem = 3, OneProperty = "3"},
                                new ItemOne {IDItem = 4, OneProperty = "4"}
                            };
                var list2 = new List<ItemTwo>
                            {
                                new ItemTwo {IDItem = 2, TwoProperty = "2"},
                                new ItemTwo {IDItem = 3, TwoProperty = "3"},
                            };
    
    
                var query = list1.Join(list2, l1 => l1.IDItem, l2 => l2.IDItem, (l1, l2) =>
                {
                    l1.OneProperty = l2.TwoProperty;
                    return l1;
                });
    see the above code where two list join and updating first list's one property called OneProperty by second list property called TwoProperty

    how to do the same with LINQ normal syntax ? like below onevar data = (from a in list1
                           join b in list2
                           on a.IDItem equals b.IDItem into c
                           from d in c.DefaultIfEmpty()
                           select new
                           {
                               a.OneProperty=(d==null ? string.Empty : d.TwoProperty)
                           }).ToList(); 

    in the above code i have not used JOIN function rather use join keyword. now tell how to customize my above code to update one property value of list1 by another property value from list2.

    please share code or customize my code to work. thanks



    Tuesday, April 9, 2019 7:36 PM

Answers

  • Linq is not a panacea its job is to query. it looks to me that what you have presented cannot be viable, because anaymous type is being  used and not a concrete type.

    join b in list2
                           on a.IDItem equals b.IDItem into c
                           from d in c.DefaultIfEmpty()
                           select new
                           {
                               a.OneProperty=(d==null ? string.Empty : d.TwoProperty)
                           }).ToList(); 


    • Marked as answer by Sudip_inn Tuesday, April 9, 2019 7:59 PM
    Tuesday, April 9, 2019 7:58 PM

All replies

  • Linq is not a panacea its job is to query. it looks to me that what you have presented cannot be viable, because anaymous type is being  used and not a concrete type.

    join b in list2
                           on a.IDItem equals b.IDItem into c
                           from d in c.DefaultIfEmpty()
                           select new
                           {
                               a.OneProperty=(d==null ? string.Empty : d.TwoProperty)
                           }).ToList(); 


    • Marked as answer by Sudip_inn Tuesday, April 9, 2019 7:59 PM
    Tuesday, April 9, 2019 7:58 PM
  • please test your code i think it will not run.

    i change like this way...it is working now

                var data = (from a in list1
                           join b in list2
                           on a.IDItem equals b.IDItem into c
                           from d in c.DefaultIfEmpty()
                           select new
                           {
                              OneProperty= a.OneProperty=(d==null ? "Not Matched" : d.TwoProperty)
                           }).ToList();
    

    Thursday, April 11, 2019 6:47 PM