gpt4 book ai didi

linq - Linq to NHibernate 中的子查询

转载 作者:行者123 更新时间:2023-12-01 14:34:55 24 4
gpt4 key购买 nike

我在 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);

为什么?

最佳答案

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

代码是这样的

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));

或者你可以试试

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

不确定上面的第二种解决方案

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

关于linq - Linq to NHibernate 中的子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9390294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com