Linq to NHibernate 返回的结果与 HQL 不同?

发布于 2024-08-23 12:34:16 字数 1400 浏览 4 评论 0原文

我有这个基本的实体设置:

public class Instrument
{
    public virtual int Id { get; set; }
    public virtual Guid? InstrumentGuid { get; set; }
    public virtual string FIPSCode { get; set; }
    public virtual IList Names {get; set;}
}

public class Name
{
    public virtual int Id {get; set;}
    public virtual string Name {get; set;}
    public virtual Instrument Instrument {get; set;}
}

映射:

public class InstrumentMap: ClassMap<Instrument>
{
    public InstrumentMap()
    {
        Id(x => x.Id);
        Map(x => x.InstrumentGuid).Not.Nullable();
        Map(x => x.FIPSCode).Not.Nullable();
        HasMany(x => x.Names).Casecade.All;
    }
}

public class NameMap : ClassMap<Name>
{
    public NameMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        References(x => x.Instrument);
    }
}

那么为什么当我执行这两个查询时会得到不同的结果?

var namelist = from name in Session.Linq()
    where name.Instrument.Id == 1
    select name;

我得到 3 个结果,2 个(其中 Instrument.Id = 1)和 1 个(其中 Instrument.Id = 4)对比:

var querystr = "select name From Name as name where name.Instrument.Id = 1";
var hqlresult = Session.CreateQuery(querystr).List();

这仅得到 2 个结果(其中 Instrument.Id = 1)。

有人可以解释一下 Linq 查询中 Id = 4 的来源吗,或者 NHibernate.Linq 还不太稳定? 谢谢!

I have this basic entity setup:

public class Instrument
{
    public virtual int Id { get; set; }
    public virtual Guid? InstrumentGuid { get; set; }
    public virtual string FIPSCode { get; set; }
    public virtual IList Names {get; set;}
}

public class Name
{
    public virtual int Id {get; set;}
    public virtual string Name {get; set;}
    public virtual Instrument Instrument {get; set;}
}

Mappings:

public class InstrumentMap: ClassMap<Instrument>
{
    public InstrumentMap()
    {
        Id(x => x.Id);
        Map(x => x.InstrumentGuid).Not.Nullable();
        Map(x => x.FIPSCode).Not.Nullable();
        HasMany(x => x.Names).Casecade.All;
    }
}

public class NameMap : ClassMap<Name>
{
    public NameMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        References(x => x.Instrument);
    }
}

So why is it that when I do these two queries do I get different results?

var namelist = from name in Session.Linq()
    where name.Instrument.Id == 1
    select name;

I get 3 results, 2 where Instrument.Id = 1 and 1 where Instrument.Id = 4 vs:

var querystr = "select name From Name as name where name.Instrument.Id = 1";
var hqlresult = Session.CreateQuery(querystr).List();

This gets only the 2 results where Instrument.Id = 1.

Could someone explain where the Id = 4 is coming from in the Linq query, or is NHibernate.Linq not quite stable yet?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海里的那抹蓝 2024-08-30 12:34:16

这听起来像是您使用的 linq 提供程序中的错误。您使用的 linq 提供程序来自 NHibernate.Contrib。该提供程序基于标准 api。此 api 可能会生成与 hql 不同的 sql。这两个查询创建的 sql 是什么?

NHibernate 主干中是一个基于 hql 的新 linq 提供程序。该 linq 提供程序将生成与 hql 相同的 sql。我在当前正在开发的应用程序中从旧的提供程序转移到了新的提供程序。它似乎比旧的有更多的功能,但它仍然是未完成的。

This sounds like a bug in the linq provider you use. The linq provider you use comes from NHibernate.Contrib. This provider is based on the criteria api. This api might generate different sql than hql. What is the sql created by both of the queries?

In the NHibernate trunk is a new linq provider based on hql. This linq provider will generate the same sql as hql. I moved from the old to the new provider in an application I currently work on. It seams to have more functionality than the old one, but it is still unfinished.

安静 2024-08-30 12:34:16

好吧,我在 Sqllite 提供程序(我用于测试)上启用了 ShowSql,并发现它正在为每个选择创建可比较的代码。

由于某种原因,仅 Linq 提供程序(2.0 和 3.0 语法均保留存储库中的最后一项,这不是 sql 查询的一部分。我通过 sqlite3.exe 重新创建了 SQL,两者之间的输出是相同的 where 子句时,结果在 Linq 和 HQL 之间是准确的!

哦,这只发生在 Id 字段上,当我引用一个不同的字段(guid)并将其用于 感谢您的所有帮助 - 当我有机会时,我会尝试新的提供程序,但现在无法使用我们产品的测试版。

Well I enabled ShowSql on the Sqllite provider (That I'm using for testing), and found that it is creating comperable code for each select.

For some reason, the Linq provider only (both with 2.0 and 3.0 syntax leaves the last item in the repository, that is not a part of the sql query. I recreated the SQL through sqlite3.exe and the output between the two was the same. Oh and it only happens for the Id fields. When I referenced a different field (guid) and used that for the where clause = the results were accurate between Linq and HQL!

So the answer is it's not supposed to I believe, and is a bug. Thanks for all the help - and when I get a chance I will try the new provider, just can't right now on the beta version of our product.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文