0

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

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You want to use Contains on the list:

return _locationData.AsQueryable().Where(z => uids.Contains(z.uid));

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.