Using List in FindAll function -C# LinQ

Viewed 114

I am having the below line of code to filter a list

items = ctx.listName.ToList().FindAll(x=>x.LocationId= locationId); //int locationId passed as parameter

Now, instead of having just one location (locationId), is there a way to have multiple locations and use them in the query?

Like instead of int locationId can we use List<int> locations and have something like this

  items = ctx.listName.ToList().FindAll(x=>x.LocationId in locations)
1 Answers

You can use LINQ Contains() extension method like this.

The Linq Contains Method in C# is used to check whether a sequence or collection (i.e. data source) contains a specified element or not. If the data source contains the specified element, then it returns true else return false

List<int> locations= // code to get location id's here

items = ctx.listName.Where(x=>locations.Contains(x.locationId)).ToList();
Related