S
Sudip_inn
Guest
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
Continue reading...
{
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
Continue reading...