【问题标题】:NHibernate 3.1 nested where clause using LINQ doesn't create proper SQL使用 LINQ 的 NHibernate 3.1 嵌套 where 子句不会创建正确的 SQL
【发布时间】:2012-03-22 19:00:06
【问题描述】:

我有以下 Nhibernate LINQ 查询:

var query = from c in session.Query<Customer>()
            where
                c.EmailAddress == customer.EmailAddress ||
                (
                    c.Address1 == customer.Address1 &&
                    c.City == customer.City &&
                    c.State == customer.State &&
                    c.Postal == customer.Postal &&
                    c.FirstName == customer.FirstName &&
                    c.LastName == customer.LastName
                )
                select c;

我希望生成的 SQL 语句如下所示:

select
    ...
from
    dbo.Customers customer0_ 
where
    customer0_.EmailAddress=@p0 or 
    (
        customer0_.Address1=@p1 
        and customer0_.City=@p2 
        and customer0_.State=@p3 
        and customer0_.Postal=@p4 
        and customer0_.FirstName=@p5 
        and customer0_.LastName=@p6;
    )

但我从调试日志中看到的是:

select
    ...
from
    dbo.Customers customer0_ 
where
    customer0_.EmailAddress=@p0 
    or customer0_.Address1=@p1 
    and customer0_.City=@p2 
    and customer0_.State=@p3 
    and customer0_.Postal=@p4 
    and customer0_.FirstName=@p5 
    and customer0_.LastName=@p6;

请注意,where 子句的地址部分没有分组。这是故意的吗?我应该以不同的方式格式化我的查询,还是这是一个错误?

【问题讨论】:

  • 我怀疑您需要升级到 3.2(或来自主干的最新版本),因为 3.1 linq 功能不完整。
  • 带有此代码的程序集引用的是 3.1(附带 Fluent Nhibernate nuget 包),但主项目引用的是 3.2,带有绑定重定向,所以实际的实时代码应该使用 3.2,没有?

标签: nhibernate


【解决方案1】:

生成的SQL是正确的;不需要括号。

【讨论】:

  • 绝对正确,但感谢您在 cmets 中包含确切解释原因的链接。
猜你喜欢
  • 1970-01-01
  • 2021-07-05
  • 2011-09-15
  • 2013-01-16
  • 2023-03-20
  • 2013-07-11
  • 1970-01-01
  • 2013-04-22
相关资源
最近更新 更多