2012-06-08 157 views
1

我试图找到包含从数据库歌厅他们之后的短语中所有单词的所有元素:在LINQ奇怪的行为为sql

string text = "ab cd 23";//the element prop2 must have all 3 word regardless their order 
var q = from c in db.myTable 
where c.Prop1 == "value" 
select c; 
string[] strings = text.Split(' '); 
foreach(string word in strings) 
{ 
     int cnt = q.Count();//first time cnt = 1500 which is correct 
     q = q.Where(c => c.Prop2.Contains(word));// Prop2 is a string 
     cnt = q.Count();//first time cnt = 460 
} 

一切正常,直到这样的:

foreach(string word in strings)// second time 
{ 
     int cnt = q.Count();//second time cnt = 14 ?? 
     q = q.Where(c => c.Prop2.Contains(word)); 
     cnt = q.Count();//first time cnt = 2 
} 

在第二个循环中没有做任何事情元素计数变化 此外,这应该只返回具有所有单词的元素,但它返回的元素只有最后一个 和第三个循环没用没有变化

对不起,我很长Q,但我是新来的LINQ

回答

3

我认为这可能是可怕的“修改关闭”错误。创建word循环变量的临时副本,并在查询中使用它。

foreach(string word in strings) { 
    var tmp = word; 
    int cnt = q.Count(); 
    q = q.Where(c => c.Prop2.Contains(tmp)); 
    cnt = q.Count();//first time cnt = 460 
} 

您应该避免使用LINQ中表达循环变量,除非你“兑现”他们马上(即调用ToList()ToArrayFirst()SingleOrDefault等),当您需要使用您的循环变量的值,做一个临时副本。原因是LINQ推迟执行查询,所以当查询得到执行时,循环变量的值发生了变化,结果将会意外更改。

+0

很快:D谢谢! – Star