I am attempting to join two lists (flist and slist) on the ID column. List definitions, class definitions, list contents, and desired results are displayed below.
List<first> flist= new List<first>();
List<second> slist= new List<second>();
public class first
{
public string name { get; set; }
public int ID{ get; set; }
public string itemAttr { get; set; }
}
public class second
{
public int ID{ get; set; }
public string itemAttr{ get; set; }
}
List contents
flist:
apples | 1
bananas| 2
trees | 3
slist:
1 | fruit
3 | not-fruit
Desired result:
flist:
apples | 1 | fruit
bananas | 2 |
trees | 3 | not-fruit
List<first> flist= new List<first>();
List<second> slist= new List<second>();
var result = from f in flist
join s in slist on f.ID equals s.ID into g
select new {
f.name,
f.ID,
itemAttr = g.Any() ? g.First().itemAttr : null
};
Try this
foreach(var f in first)
{
foreach(var s in second)
{
if(f.ID == s.ID)
{
f.fAttr = item.itemAtrr;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With