c# - Intersection of two lists using LINQ -


i have user called admin has list of companys. want return list of users have 1 or more of same companys. using linq using query i'm not sure why not working. don't understand .any() if don't include it, program has syntax errors. here attempt:

public list<user> getusers(user admin) {     return users.where(user=>user.companys.intersect(admin.companys)).any()).tolist();  } 

edit: people in comments taking overriding equals company object , correct might able easier. reason need override equals because .net doesn't know how find equality in object created. need program in how let know. know how find equality in id times however.

edit 2: intersect way go because want compare list list

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();         } 

so contains search list see if single value in list. because searches single value won't work this.

intersect return intersections of 2 lists. example [1,2,3] [2,3,4] give [2,3].

where requires boolean function evaluation. give me values in list function given returns true. when give [2,3] complains. says there results in list. [2,3].any() returns true, satisfying where.

contains doesn't return intersection of list, tells true of false, value exist

hope helps.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -