问题描述
也许是一个简单的问题,我正在尝试从名称列包含所有搜索词数组的表中获取结果.我正在创建一个查询并遍历我的搜索字符串,每次分配 query = query.Where(...);.似乎只使用了最后一个术语,我想是因为我每次都试图限制同一个字段.如果我在每次迭代中调用 .ToArray().AsQueryable() ,我可以获得我正在寻找的累积限制行为,但是是否有一种简单的方法可以仅使用延迟运算符来做到这一点?
谢谢!
推荐答案
如果你正在做类似的事情:
foreach (int foo in myFooArray) { query = query.where(x => x.foo == foo); }
...那么它将只使用最后一个,因为每个 where 条件都包含对 'foo' 循环变量的引用.
如果您正在这样做,请将其更改为:
foreach (int foo in myFooArray) { int localFoo = foo; query = query.where(x => x.foo == localFoo); }
...一切都会好起来的.
如果这不是正在发生的事情,请提供您正在做的事情的代码示例...
问题描述
Maybe a simple question, I'm trying to get a result from a table where the Name column contains all of an array of search terms. I'm creating a query and looping through my search strings, each time assigning the query = query.Where(...);. It appears that only the last term is being used, I supposed because I am attempting to restrict the same field each time. If I call .ToArray().AsQueryable() with each iteration I can get the cumlative restrinction behavior I'm looking for, but it there an easy way to do this using defered operators only?
Thanks!
推荐答案
If you're doing something like:
foreach (int foo in myFooArray) { query = query.where(x => x.foo == foo); }
...then it will only use the last one since each where criteria will contain a reference to the 'foo' loop variable.
If this is what you're doing, change it to:
foreach (int foo in myFooArray) { int localFoo = foo; query = query.where(x => x.foo == localFoo); }
...and everything should be fine again.
If this is not what is happening, please provide a code sample of what you're doing...