linq - foreach inside a foreach

hi
I have an "roleId" - and i want to select all "subRoles" which have this "roleId" from a list of subRoles. Something like...

#####################
#####################
pseudo code....

var roleId = 1;
var selectedSubRoles = subRoles.Select(sr => sr.RoleType.Id = roleId)
//but this just returns a list of int's not a list of "SubRole" objects
//so doesnt work

Then i want to search this list of "selectedSubRoles"  to see if the "SubRole.Id" matches - any int's i have in another list.
Something like...

List<SubRole> secondListOfSelectedSubRoles = new List<SubRole>

foreach(SubRole subRole in selectedSubRoles)
{
foreach(int i in MyListOfIntegers)
{
  if (subRole.Id = i)
  {
      secondListOfSelectedSubRoles.Add(subRole);
  }
}

end of code
#####################
#####################

and so i want to be left with "secondListOfSelectedSubRoles"
and thats it!  

can anyone help, im just starting out with linq and finding it abit tricky

thanks



LVL 1
MrKevorkianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard LeeSoftware EnthusiastCommented:
What you want is:

var selectedSubRoles = subRoles
   .Where(sr => sr.RoleType.Id == roleId)
   .Select(sr => sr) // Not necessary since the result set is not modified
   .ToArray(); // If you don't want deferred execution

DaTribe
0
MrKevorkianAuthor Commented:
thanks!  how about the second bit?!
0
Richard LeeSoftware EnthusiastCommented:
Ok missed that when I read your question :). Some clarity when writing these question would be suggested.

Here is the other bit:

var selectedSubRoles = subRoles
    .Where(sr => sr.RoleType.Id == roleId && MyListOfIntegers.Contains(sr.Id))
    .Select(sr => sr) // Not necessary since the result set is not modified
    .ToArray(); // If you don't want deferred execution

DaTribe
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.