How do I do this in linq?
var p = new pmaker();
foreach (var item in itemlist)
{
var dlist = new List<Dummy>();
foreach (var example in item.examples)
{
dlist.Add(example.GetDummy());
}
p.AddStuff(item.X,item.Y,dlist);
}
// .. do stuff with p
How about:
var qry = from item in itemlist
select new {item.X, item.Y,
Dummies = item.examples.Select(
ex => ex.GetDummy())
};
foreach (var item in qry)
{
p.AddStuff(item.X, item.Y, item.Dummies.ToList());
}
Not sure it is much clearer like this, though... personally I think I might just use the original foreach version... maybe splitting out the GetDummy bit:
foreach (var item in itemlist)
{
var dlist = item.examples.Select(ex => ex.GetDummy()).ToList();
p.AddStuff(item.X,item.Y,dlist);
}
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