Intersecting Two Lists Using LINQ

I have a user called admin who has a list of companies. I want to return a list of users who have one or more companies. I am using Linq using this query, but I'm not sure why it is not working. I really do not understand what .Any () does, but if I do not enable it, the program has syntax errors. Here is my attempt:

public List<User> GetUsers(User admin)
{
    return Users.Where(user=>user.Companys.Intersect(admin.Companys)).Any()).ToList();

}
+4
source share
2 answers

EDIT: , , - . , equals, , .Net , . , . , ID .

2: Intersect Any - ,

public List<User> GetUsers(User admin)
{
    var adminCompanyIDs = admin.Companys.Select(c => c.ID);
    return Users.Where(user=>user.Companys.Select(c => c.ID).Intersect(adminCompanyIDs).Any()).ToList();        
}

, , - . , .

. , [1,2,3] [2,3,4] [2,3].

. , true. , [2,3], . , . [2,3].Any() true, Where.

, True of False,

, .

+1

return Users.Where(user=>user.Companys.Select(c => c.ID).Intersect(admin.Companys.Select(a => a.id)).Any())
        .ToList();
+2

All Articles