c#


Different version of Intersect() in LINQ


There is a User entity and I have a list of User objects. User entity looks like this :
public class User{
public int Id {get; set;}
public List<Group> Groups {get; set;}
}
And Group class looks like this :
public class Group{
public int Id {get; set;}
public string GroupName {get; set;}
}
Finally I have a list of string which is like this :
List<string> GroupNames;
I have a list of User objects. I want to pick User objects whose Groups contain a Group object which has their name inside the list GroupNames.
For example, if GroupNames contain NYC, Chicago, Seattle, I want to pick Users whose Groups' GroupName equals to any of these cities. In code It looks like this :
List<string> GroupNames = new List<string> { "NYC", "Chicago","Seattle"};
var filteredUsers = Users.Where(x => x.Groups....)// I don't know what to write in this query.
Can you help me write this query? I appreciate any help, thanks.
Linq should slove this easy by combining Any() with Contains()
var filteredUsers = Users.Where(x => x.Groups.Any(y => GroupNames.Contains(y.GroupName)));
You can do it by using Any() and Contains():
var filteredUsers = Users.Where(t => t.Groups.Any(q => GroupNames.Contains(q.GroupName));
This is almost identical to fubo's answer.
Hope it helps!
Another possibility is using the Intersect() + Any() (but note the use of .Select(x => x.GroupName) to select only the GroupName)
var filteredUsers = Users.Where(x => x.Groups.Select(x => x.GroupName).Intersect(GroupNames).Any());

Related Links

c# Script Error "Line3 is a' variable' but is used like a method - Help Understand
How Can I Use The “GeckoInputElement ”
How to map a 3D array to a linear array?
How setting session expired time use code on ASP.NET MVC5?
How to always get the full number or rounded up (always) number c#
Determine if event is static using reflection
Structuremap all instances of abstract
c# - How to access a variable from outside its class in a method in some other class?
WPF Custom window invisible
How to check date between 2 dates (not SQL)
Winform : Set Password for SQL Server connection string in app.config programatically
Getting Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool
How to get Scroll to work with a Vertically oriented Hub Control Windows Phone 8.1 App
How to write query to generate XML file from SQL Server table
Load winform from class library from string
how to display binary image to image box? [duplicate]

Categories

HOME
optimization
xamarin.forms
dynamic
nltk
salesforce-lightning
bitbucket
multipartform-data
normals
google-app-maker
distinct
watson-dialog
taxonomy
samba
orange
novacode-docx
legend-properties
keystone
xna
cherrypy
smali
connection-pooling
prepros
sasl
utc
android-6.0-marshmallow
formtastic
typeclass
federated
vtd-xml
foxpro
storefront
rtos
traitsui
webmock
zimbra
javax
incapsula
powerbi-embedded
android-geofence
aweber
console.readline
jibx
queryover
office-interop
susy
piranha-cms
automapper-4
pyopenssl
history
noise
skype-bots
dotnet-cli
vertex
xamarin-test-cloud
selection-sort
fileopendialog
stripes
android-music-player
altova
std
jquery-draggable
event-tracking
firewire
cakephp-2.6
teamwork
stderr
gcc-warning
chunks
vundle
windows-search
switchpreference
amzi-prolog
angular-file-upload
visual-studio-setup-proje
mixradio
xml-validation
myrrix
ie-developer-tools
octokit
ironworker
violation
extensibility
qt5.1
m3u
motordriver
django-admin-tools
wxperl
httppostedfilebase
xpolog
overlays
xhtmlrenderer
conditional-operator
uccapi
pkcs#7
morfik
script-tag

Resources

Encrypt Message