tags:

views:

765

answers:

5

Hey guys,

I've been looking on google but not finding anything that does the trick for me.

as you know SQL has a "where x in (1,2,3)" clause which allows you to check against multiple values. I'm using linq but I can't seem to find a piece of syntax that does the same as the above statement.

I have a collection of category id's (List) against which I would like to check

I found something that uses the .contains method but it doesn't even build.

any help would be greatly appreciated...

my regards

J.

+1  A: 

Here's an article illustrating the approach. You should indeed use the Contains method over your collection which will be translated into IN clause.

Darin Dimitrov
+5  A: 

You have to use the Contains method on your id list:

var query = from t in db.Table
            where idList.Contains(t.Id)
            select t;
CMS
I have one follow up though.the column of id's is nullable (which I forgot and fixed by using the value property)what happens if the value is null?
Jan W.
If t.Id value is null then result won't include this record.
Soul_Master
A: 

The syntax is below:

IEnumerable<int> categoryIds = yourListOfIds;

var categories = _dataContext.Categories.Where(c => categoryIds.Contains(c.CategoryId));

The key thing to note is that you do the contains on your list of ids - not on the object you would apply the in to if you were writing sql.

David Hall
A: 

Let's see first link in Google Search or look at this. Why you can't find it in Google Search?

Soul_Master
SOIYF - StackOverflow Is Your Friend. * I wonder if anyone has registered "letmestackthatforyou.com" *
tsilb
Note the downvote was cuz you're trolling.
tsilb
A: 

How about if my idList is an integer?

Yaz

Yazid