简体   繁体   English

在Linq to NHibernate中测试状况的最佳方法是什么?

[英]What is the best way to test a condition in Linq to NHibernate?

The Any() linq function seems to load all of the entity's columns even though they're not needed. Any()linq函数似乎加载了实体的所有列,即使它们并不需要。

The following code: 如下代码:

if(Session.Query<Project>().Any(p=>p.ID == projectID && p.ProjectOwner.Responsible == CurrentUserID))
    // Current user is the responsible for this project

Generates the following SQL: 生成以下SQL:

select TOP (1) project0_.ProjectID                          as ProjectID7_,
               project0_.DateCreated                        as DateCrea2_7_,
               project0_.Type                               as Type7_,
               project0_.ProjectOwner_FK                    as ProjectOy8_7_,
               project0_.Address_FK                         as Address9_7_,

**[Snip -- the statement selects all of Project's columns]**

from   [Project] project0_
       inner join [OrganizationProject] organizati1_
         on project0_.ProjectOwner_FK = organizati1_.OrganizationProjectID
where  project0_.ProjectID = 1 /* @p0 */
       and organizati1_.Responsible_FK = 1 /* @p1 */

However, the following code: 但是,以下代码:

if(Context.Projects.Where(p=>p.ID == projectID && p.ProjectOwner.Responsible == CurrentUserID).Count() == 1)
    // Current user is the responsible for this project

Generates the following sql, which is what is expected: 生成以下sql,这是预期的结果:

select cast(count(*) as INT) as col_0_0_
from   [Project] project0_
       inner join [OrganizationProject] organizati1_
         on project0_.ProjectOwner_FK = organizati1_.OrganizationProjectID
where  project0_.ProjectID = 1 /* @p0 */
       and organizati1_.Responsible_FK = 1 /* @p1 */

The Count() method does what is expected, but it is a bit less straightforward. Count()方法可以完成预期的工作,但不太直接。

Is the Any() behavior considered normal or is it a bug? 是Any()行为被认为是正常的还是错误? It doesn't seem optimal to me, but maybe loading the entity isn't really slower than asking SQL to return the count? 这对我来说似乎不是最佳选择,但是也许加载实体并不比要求SQL返回计数慢吗?

In light of this, what is considered to be the best way to test a condition in Linq to NHibernate? 有鉴于此, 什么是在Linq to NHibernate中测试条件的最佳方法?

It was interesting but did you tryed: 这很有趣,但是您是否尝试过:

if(Session.Query<Project>()
   .FirstOrDefault(p=>p.ID == projectID 
                 && p.ProjectOwner.Responsible == CurrentUserID) != null)

I think it would be faster from your current options. 我认为从您当前的选择中可以更快。 In fact it doesn't checks for all items (like count) Also it doesn't fetch all data. 实际上,它不会检查所有项(例如count),也不会获取所有数据。

Something like this should create a query with only a single column: 这样的事情应该创建一个仅包含一列的查询:

if(Session
    .Query<Project>()
    .Where(p=>p.ID == projectID && p.ProjectOwner.Responsible == CurrentUserID)
    .Select(p=>new { pID = p.ID })
    .Any())
{
   ...
}

Projection to an anonymous type allows NHibernate to fetch only the specified column (ID, for example). 投影到匿名类型允许NHibernate仅获取指定的列(例如ID)。

But it is usually more suitable when there is a significant number of rows to retrieve. 但是,当要检索的行数量很多时,它通常更适合。

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

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