Thursday, 12 September 2013

Eliminate loop and use Linq instead

Eliminate loop and use Linq instead

Say I have a List as below:
List<R> lstR = GetR();
Now I want a Linq statement to get menus assigned to R, I achieved this by
using a loop and then using Linq to get the menus as below:
List<int> ids = new List<int>();
foreach (R r in lstR)
{
ids.Add(r.Id);
}
menu = (from s in db.Menu
where ids.Contains(s.R.Id)
select s.MenuText).Distinct();
Now as far as I know the above is two loop(Linq is using internal loop).
would I be able to combine these two statements i.e. not do the first loop
to get the ids?

No comments:

Post a Comment