Linq到NHibernate vs.ICriteria [英] Linq to NHibernate vs. ICriteria

查看:61
本文介绍了Linq到NHibernate vs.ICriteria的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常使用LINQ,尤其是LINQ-to-Objects,因此我对LINQ相当熟练.

I use LINQ a lot in general, especially LINQ-to-Objects, hence I'm rather fluent in LINQ.

我正在考虑使用LINQ-to-NHibernate作为NHibernate项目的查询语言.当我编写一些测试时,我注意到LINQ-to-NHibernate的查询与ICriteria的查询不同.因为我更喜欢使用LINQ,所以我想问问是否有人知道类似的区别,或者我是否应该只关心一般的性能(就我所知,高性能操作还是需要对NHibernate进行一些调整.它).请参见以下示例:

I was considering to use LINQ-to-NHibernate as the query language for my NHibernate project. When I wrote some tests, I noticed that LINQ-to-NHibernate doesn't make the same query as ICriteria. Since I'd prefer to use LINQ, I'd like to ask if anyone knows of similar differences, or should I just not bother about performance in general (The high-performance operations would need some tweaking with NHibernate anyway as far as I get it). See the following example:

var query = (from inputItem in session.Linq<InputItem>()
             where inputItem.Project == project select inputItem).First();

为我提供了以下SQL:

gives me the following SQL:

SELECT this_.ID as ID0_1_, this_.Name as Name0_1_, this_.Project_id as Project3_0_1_, project1_.ID as ID1_0_, project1_.Name as Name1_0_
    FROM "InputItem" this_ left outer join "Project" project1_ on this_.Project_id=project1_.ID
    WHERE this_.Project_id = @p0 limit 1;@p0 = 1, @p1 = 1

var criteria = session.CreateCriteria<InputItem>();
criteria.Add(Expression.Eq("Project", project));
criteria.SetMaxResults(1);
criteria.List();

给予

SELECT this_.ID as ID0_0_, this_.Name as Name0_0_, this_.Project_id as Project3_0_0_
    FROM "InputItem" this_
    WHERE this_.Project_id = @p0 limit 1;@p0 = 1, @p1 = 1

很显然,不需要LEFT JOIN.

Clearly, the LEFT JOIN wouldn't be necessary.

我的LINQ查询出问题了吗?还是仅仅是一个限制?我首先要担心吗?

Is something wrong with my LINQ query or is it just a limitation? And should I worry about it in the first place?

冰冷

我尝试将LINQ语句更改为以下内容:

I tried changing the LINQ statement to the following:

var query = (from inputItem in session.Linq<InputItem>()
             where inputItem.Project.ID == project.ID
             select inputItem).First();

但是生成的SQL是相同的.

the generated SQL is the same, though.

推荐答案

在我看来,NHibernate.Linq目前不支持此优化.我认为您需要使用条件查询或HQL,或者等到完全集成的LINQ提供程序发布(我认为是针对NHib v3的).

It looks to me as if NHibernate.Linq doesn't support this optimisation right now. I think you'll need to use a criteria query, or HQL, or wait until the fully integrate LINQ provider is released (slated for NHib v3 I think).

干杯, 约翰

这篇关于Linq到NHibernate vs.ICriteria的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆