[英]C# strange lambda behavior
Could someone point out why this could be happening: 有人可以指出为什么会发生这种情况:
I am using NHibernate
and the Linq
provider for it. 我正在使用NHibernate
和Linq
提供程序。
The code that fails is listed here: 此处列出了失败的代码:
var sequence = session.Query<T>();
var wtfSequence = sequence.Where(x => true);
var okaySequence = sequence.Where(x => x.Id > 0);
Debugging shows that sequence
(which is an IQueryable<T>
) after this contains 2 elements, which were added to the database. 调试显示sequence
(其为IQueryable<T>
)之后包含2个元素,这些元素已添加到数据库中。
I expect the first Where
statement to yield all elements from that sequence, but unfortunately it leaves 0 elements. 我期望第一个Where
语句从该序列中产生所有元素,但不幸的是它留下了0个元素。
The second Where
statement, on the contrary, actually yields 2 elements as it should work. 相反,第二个Where
语句实际上产生了2个元素,因为它应该起作用。
Here are the NHibernate -> Sqlite
queries for the first and second Where
statements. 以下是第一个和第二个Where
语句的NHibernate -> Sqlite
查询。
NHibernate: select cast(count(*) as INTEGER) as col_0_0_ from "BinaryUnitProxy_IndicatorUnitDescriptor" binaryunit0_ where @p0='true';@p0 = 'True' [Type: String (0)]
NHibernate: select cast(count(*) as INTEGER) as col_0_0_ from "BinaryUnitProxy_IndicatorUnitDescriptor" binaryunit0_ where binaryunit0_.Id>@p0;@p0 = 0 [Type: Int32 (0)]
Now, if I test the same code with my InMemoryRepository
, which stores every entity in a simple list, the (x => true)
works absolutelty fine. 现在,如果我用我的InMemoryRepository
测试相同的代码,它将每个实体存储在一个简单的列表中,那么(x => true)
可以了。
So - why does this happen when using NHibernate
? 那么 - 为什么在使用NHibernate
时会发生这种情况? Is this a bug or I am doing something wrong? 这是一个错误还是我做错了什么?
Thank you. 谢谢。
I don't know NHibernate, but the problem is obvious from the generated SQL: Your database does not consider true (lowercase t) equal to True (uppercase T). 我不知道NHibernate,但问题从生成的SQL中显而易见:您的数据库不认为true(小写t)等于True(大写T)。 In SQL server you can change this by modifying the database collation (which is a really bad idea unless you want case insensitivity for other reasons). 在SQL Server中,您可以通过修改数据库排序规则来更改此设置(除非您因其他原因需要不区分大小写,否则这是一个非常糟糕的主意)。
My guess is this is a bug in NHibernate that you need to work around. 我猜这是你需要解决的NHibernate中的一个错误。 Test t => 1 == 1
instead of t => true
, which might work depending on how the NHibernate code is written. 测试t => 1 == 1
而不是t => true
,这可能会起作用,具体取决于NHibernate代码的编写方式。
My guess is that this is a bug in NHibernate based on the SqLite output that you show. 我的猜测是,这是NHibernate中基于您显示的SqLite输出的错误。 You could try X => X.Id == X.Id
rather than X => true
and see if that works. 您可以尝试X => X.Id == X.Id
而不是X => true
,看看是否有效。
Looks like a bug to me. 对我来说看起来像个错误。 Its converting a boolean operation to a string evaluation, and even that's screwed up, as it sets up the query with true
and evaluates using True
, so a case-sensitive test would fail. 它将布尔操作转换为字符串评估,甚至搞砸了,因为它使用true
设置查询并使用True
进行求值,因此区分大小写的测试将失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.