views:

59

answers:

2

I'm using LINQtoSQL and I want to return a list of matching records for a CSV contains a list of IDs to match. The following code is my starting point, having turned a CSV string in a string array, then into a generic list (which I thought LINQ would like) - but it doesn't:

Error

Error 22 Operator '==' cannot be applied to operands of type 'int' and 'System.Collections.Generic.List<int>' C:\Documents and Settings\....\Search.cs 41 42 C:\...\

Code

    DataContext db = new DataContext();

    List<int> geographyList = new List<int>( Convert.ToInt32(geography.Split(',')) );

        var geographyMatches = from cg in db.ContactGeographies
                               where cg.GeographyId == geographyList
                               select new { cg.ContactId };

Where do I go from here?

+1  A: 
var geographyMatches = from cg in db.ContactGeographies
                               where geographyList.Contains(cg.GeographyId)
                               select new { cg.ContactId };
HackedByChinese
This does the trick - this syntax seems odd to me, but I look at LINQ strange SQL eyes which it blinding me to these type of approaches
Peter Bridger
A: 

Like the error says, in where cg.GeographyId == geographyList you're trying to compare int to a List<int>. They're totally different types. I think you want to do geographyList.Contains(cg.GeographyId) or some other operation like that.

Benny Jobigan