Reputation: 23
I have List _locationData that contains a unique identifier uid which is a string. I want to use a LINQ query to return a List that have a uid that is contained within a List uids.
public IQueryable<Location> FindAll(List<string> uids)
{
return _locationData.AsQueryable().Where(z => z.uid.Any(v => uids.Equals(v)));
}
Upvotes: 0
Views: 98
Reputation: 4542
You can also try a join like this:
public IQueryable<Location> FindAll(List<string> uids)
{
return _locationData.AsQueryable().Join(uids, l => l.uid.ToString(), s => s, (l, s) => l);
}
Upvotes: 0
Reputation: 124
If you don't strictly need the result to be IQueryable and that was just a convenience for what linq was returning from where, you could do this:
private List<Location> FindAll(List<string> uids)
{
return _locationData.FindAll(location => uids.Contains(location.uid));
}
Upvotes: 0
Reputation: 152501
You want to use Contains
on the list:
return _locationData.AsQueryable().Where(z => uids.Contains(z.uid));
Upvotes: 0
Reputation: 53958
You could try this one:
public IQueryable<Location> FindAll(List<string> uids)
{
return _locationData.AsQueryable().Where(z => uids.Contains(z.uid));
}
This checks if for each element in your location data, it's contained in the list uids
. if so, then the location will be included in the result that will be returned.
Upvotes: 2