LINQ to SQL中的多个延迟的WHERE子句表达式 [英] Multiple Defered WHERE clause expressions in LINQ to SQL
问题描述
也许是一个简单的问题,我试图从表中获取结果,该表的名称"列包含所有搜索项数组.我正在创建一个查询并遍历我的搜索字符串,每次分配查询= query.Where(...);.我猜想似乎只使用了最后一个术语,因为我每次都试图限制相同的字段.如果我在每次迭代中都调用.ToArray().AsQueryable(),则可以获得所需的累积重排行为,但是有一种简单的方法可以仅使用延迟运算符来做到这一点吗?
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?
谢谢!
推荐答案
如果您正在执行以下操作:
If you're doing something like:
foreach (int foo in myFooArray)
{
query = query.where(x => x.foo == 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...
这篇关于LINQ to SQL中的多个延迟的WHERE子句表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!