lambda expression for multiple countries in Linq to Entity
I have a field called Country. Now I have to filter the records based on the matching country.
When I have to filter the records for single country i use the lambda expression as below.
string strCountry = "USA";
var data = entities.Documents.Where(p => p.Country == strCountry);
Now I have the list of countries as below. I am 开发者_StackOverflowwondering how to filter the records for this Contry List.
List<string> strCountry = new CacheUser().GetUserCountry();
Appreciate your responses.
Thanks
Try:
List<string> countries = ...;
var data = entities.Documents.Where(p => countries.Contains(p.Country));
This should result in an "IN" query in SQL. However, you should be aware that this may fail once you get a large number of countries in the list. I assume you can't do this with a join instead? The list of countries is only known at the client side?
精彩评论