tags:

views:

63

answers:

4

Here is my code

if (catid != 0)
            posts = posts.Where(x => x.catid IN '1,8,2,109,23');

The in in this code shows as a syntax error. Is there a way to fix this

+7  A: 

You must use another list to compare too.

List<int> cadIdFoundList = new List<int>();

cadIdFoundList.Add(1);
cadIdFoundList.Add(8);
// etc. . . 

posts.Where(x => cadIdFoundList.Contains(x.catId));
David Basarab
Cool. Thanks..It works
Luke101
+3  A: 
int[] ids = new int[] { 1, 8, 2, 109, 23 };
var query = posts.Where(x => ids.Contains(x.catid));

Rob Conery has discussed this topic before.

Jason
+2  A: 

Or even more simple:

var query = posts.Where(x => new[] { 1, 8, 2, 109, 23 }.Contains(x.catid));
Terry Donaghe
It's a shame we have to put that new[] there. Wouldn't it be cool if we could just do {1, 7, 3, 5}.Contains instead? :D
Terry Donaghe
+1  A: 

Maybe something more like:

HashSet<int> categories = new HashSet<int>() { 1, 2, 8, 23, 109};
posts = posts.Where(post => categories.Contains(post.catid));
Sapph