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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Richard LeeConnect With a Mentor Software 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
 
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
All Courses

From novice to tech pro — start learning today.