Linq to NHibernate 中的子查询
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 sub_q 上应用 ToList 方法可能会解决您的问题,因为可能存在 linq different 执行问题。
代码类似于
OR 您可以尝试
不确定上述第二个解决方案,
但我认为您必须需要执行
ToList()
来解决执行不同的问题。Apply ToList method on sub_q might resolve your issue because there might be a problem of linq differed execution..
code is like
OR you can try
not sure about above second soltuion
but i think you must need to do
ToList()
to resolve issue of differed execution..我的问题通过将 sub_q 中的
x
变量重命名为xx
中的
x
来解决,子查询中的变量被查询中的x
覆盖。通过更改名称,我的问题就解决了。My problem resolved by rename
x
variable in sub_q toxx
x
in subquery override byx
in query. By change it's name, my problem is resolved.