简体   繁体   English

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

[英]Linq to NHibernate returns different results than HQL?

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: 我得到3个结果,2表示Instrument.Id = 1和1表示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. 这只得到2个结果,其中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? 有人可以解释在Linq查询中Id = 4的来源,还是NHibernate.Linq还不是很稳定呢? Thanks! 谢谢!

This sounds like a bug in the linq provider you use. 这听起来像你使用的linq提供程序中的错误。 The linq provider you use comes from NHibernate.Contrib. 您使用的linq提供程序来自NHibernate.Contrib。 This provider is based on the criteria api. 此提供程序基于API标准。 This api might generate different sql than hql. 这个api可能会生成与hql不同的sql。 What is the sql created by both of the queries? 这两个查询创建的sql是什么?

In the NHibernate trunk is a new linq provider based on hql. 在NHibernate中,trunk是一个基于hql的新linq提供程序。 This linq provider will generate the same sql as hql. 这个linq提供程序将生成与hql相同的sql。 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. 它接缝的功能比旧的功能更多,但它仍未完成。

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. 我在Sqllite提供程序上启用了ShowSql(我正在用于测试),并发现它正在为每个选择创建可匹配的代码。

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! 出于某种原因,只有Linq提供程序(使用2.0和3.0语法都保留了存储库中的最后一项,这不是sql查询的一部分。我通过sqlite3.exe重新创建了SQL,两者之间的输出是相同的哦,它只发生在Id字段中。当我引用一个不同的字段(guid)并将其用于where子句时= Linq和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. 感谢所有的帮助 - 当我有机会尝试新的提供商时,就在我们产品的测试版本上就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM