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);
精彩评论