C # Linq SubQuery / In Object

I have 2 objects that contain common list properties. IE:

public class User
{
    public string Sid { get; set; }
    public List<Group> Groups { get; set; }
}

public class Section
{
    public string Sid { get; set; }
    public List<Group> Groups { get; set; }
}

From my BLL I get a general list of sections List mySections = SectionDB.getList ();

and my User object contains user information User myUser = UserDB.getInfo (sid);

Using linq for objects, is it possible to make a query that returns all sections that have at least one group in the user class of the group?

Any help?

+3
source share
2 answers
from section in mySections
from sectionGroup in section.Groups
where myUser.Groups.Any(userGroup => userGroup == sectionGroup)
select section

I would rather go for any, since you use an iterator much more efficiently

+2
source
var sections = mySections.Where(x => x.Groups.Intersect(myUser.Groups)
    .Any()).ToList();

( , Group, Equals/GetHashCode Group)

+2

Source: https://habr.com/ru/post/1729062/


All Articles