2010-03-09 66 views
0

我正在使用LINQtoSQL,我想返回CSV匹配記錄的列表,其中包含要匹配的ID列表。下面的代碼是我的出發點,在一個字符串數組已經變成一個CSV字符串,然後進入一個泛型列表(我認爲LINQ想) - 但事實並非如此:LINQ-to-SQL:使用CSV進行搜索

錯誤

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:\...\ 

代碼

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 }; 

我在哪裏何去何從?

回答

1
var geographyMatches = from cg in db.ContactGeographies 
           where geographyList.Contains(cg.GeographyId) 
           select new { cg.ContactId }; 
+0

這樣做的竅門 - 這個語法對我來說看起來很奇怪,但是我看看LINQ奇怪的SQL眼睛,它使我對這些類型的方法 – 2010-03-09 11:23:56

0

像錯誤說,在where cg.GeographyId == geographyList你想比較intList<int>。他們是完全不同的類型。我想你想要做geographyList.Contains(cg.GeographyId)或其他一些類似的操作。