I have a linq issue that I am trying to solve, I have some users who can be part of multiple groups, now I am able to return which users belongs to a single group like so:
List<Student> students = new List<Student>();
public List<Student> ReturnStudentByGroupName(string groupName)
{
List<Student> student = (from g in students
where
(from t in g.StudentGroup where t.GroupName == groupName select t).Count() > 0
select g).ToList();
return student;
}
My problem now tho is I need to find the common users of multiple groups? For instance who are the common members of group A and group B. Im not looking for a list of users of these two groups it should only return users if they belong to both groups.
Does anyone know how to do this using two strings as inputs i.e string firstgroupName, string secondgroupName. Then return common students?
Well you said you only want to return a list of users that belong to Both groups A and B, so naturally you just need to modify a where statement with two conditions instead of one.
Here are a couple methods for you, one of which takes two arguments in the method (What you asked for) and another that takes a list of groups (if you need to get from three, instead of two, etc)
EDIT:
I thought i would throw this extra method in there as well, just for fun. It compiles a list of all the groups and their members.