Linq to NHibernate 中的子查询

发布于 2025-01-07 17:29:19 字数 711 浏览 2 评论 0原文

我在 NHibernate 3.1 中通过 Linq 进行查询:

public IList<Person> Search()
{
    var sub_q = SessionInstance.Query<Person>().Where(x => x.Id < 6).Select(x => x.Id);

    var q = SessionInstance.Query<Person>();
    q = q.Where(x => sub_q.Contains(x.Id));

    return q.ToList<Person>();
}

Id 列是数据库中的主键 这个子查询不起作用。使用子查询的查询计数等于不使用子查询的查询计数。

我的带子查询的查询计数 : 52 //正确的计数是 5

我的不带子查询的查询计数 : 52

为什么?

更新: 我的问题通过将 sub_q 中的 x 变量重命名为 xx 得到解决,

 var sub_q = SessionInstance.Query<Person>().Where(xx => xx.Id < 6).Select(xx => xx.Id);

为什么?

I have a query by Linq in NHibernate 3.1:

public IList<Person> Search()
{
    var sub_q = SessionInstance.Query<Person>().Where(x => x.Id < 6).Select(x => x.Id);

    var q = SessionInstance.Query<Person>();
    q = q.Where(x => sub_q.Contains(x.Id));

    return q.ToList<Person>();
}

Id column is primary key in Database
This sub-query does not work. Count of my query with sub-query equals by count of my query without use sub-query.

Count of my query whit sub-query : 52 //Correct count is 5

Count of my query without sub-query : 52

Why?

Updated:
My problem resolved by rename x variable in sub_q to xx

 var sub_q = SessionInstance.Query<Person>().Where(xx => xx.Id < 6).Select(xx => xx.Id);

Why?

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

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

发布评论

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

评论(2

淡忘如思 2025-01-14 17:29:19

在 sub_q 上应用 ToList 方法可能会解决您的问题,因为可能存在 linq different 执行问题。

代码类似于

var sub_q = SessionInstance.Query<Person>()
                   .Where(x => x.Id < 6).Select(x => x.Id).ToList(); 
    var q = SessionInstance.Query<Person>();    
 q = q.Where(x => sub_q.Contains(x.Id)); 

OR 您可以尝试

q = q.Where(x => (SessionInstance.Query<Person>()
                 .Where(x => x.Id < 6).Select(x => x.Id)).Contains(x.Id)); 

不确定上述第二个解决方案,

但我认为您必须需要执行 ToList() 来解决执行不同的问题。

Apply ToList method on sub_q might resolve your issue because there might be a problem of linq differed execution..

code is like

var sub_q = SessionInstance.Query<Person>()
                   .Where(x => x.Id < 6).Select(x => x.Id).ToList(); 
    var q = SessionInstance.Query<Person>();    
 q = q.Where(x => sub_q.Contains(x.Id)); 

OR you can try

q = q.Where(x => (SessionInstance.Query<Person>()
                 .Where(x => x.Id < 6).Select(x => x.Id)).Contains(x.Id)); 

not sure about above second soltuion

but i think you must need to do ToList() to resolve issue of differed execution..

ぃ弥猫深巷。 2025-01-14 17:29:19

我的问题通过将 sub_q 中的 x 变量重命名为 xx

 var sub_q = SessionInstance.Query<Person>().Where(xx => xx.Id < 6).Select(xx => xx.Id); 

中的 x 来解决,子查询中的变量被查询中的 x 覆盖。通过更改名称,我的问题就解决了。

My problem resolved by rename x variable in sub_q to xx

 var sub_q = SessionInstance.Query<Person>().Where(xx => xx.Id < 6).Select(xx => xx.Id); 

x in subquery override by x in query. By change it's name, my problem is resolved.

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