开发者

Modify LINQ expression to accept IN clause

I'm trying to modify the following code line to accept a good old fashioned IN clause.

var searchResults =  (
    from s in allSites
    orderby s.Si开发者_JAVA百科teDescription
    where s.SiteDescription.StartsWith(siteDescription) &&
          s.SiteLocation != null
    select s);

I've looked at examples but not getting anywhere fast.

I'd like to add something like:

WHERE s.SiteStatusId IN (3,4,5)

How can I do this?


Try:

var ids = new [] { 3, 4, 5 };

var searchResults = from s in allSites
                    where ids.Contains(s.Id)
                    select s;

I think that does what you want, but it's been a while since I've done LINQ to SQL...


To generate the IN clause, use Contains() on some collection.

var query = from s in allSites
            orderby s.SiteDescription
            where s.SiteDescription.StartsWith(siteDescription)
               && s.SiteLocation != null
               && new[] { 3, 4, 5 }.Contains(s.SiteStatusId)
            select s;


 var siteStatusIds = new [] { 3, 4, 5 };
 var searchResults =  (
     from s in allSites
     orderby s.SiteDescription
     where s.SiteDescription.StartsWith(siteDescription) &&
           s.SiteLocation != null &&
           siteStatusIds.Contains(s.SiteStatusId)
     select s);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜