开发者

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜