动态linq-to-nhibernate查询问题
假设我有类 Foo
和 Bar
如下:
public class Foo
{
public string F1 {set; get;}
public string F2 {set; get;}
public Bar ContainerBar {set; get;}
}
public class Bar
{
public string B1 {set; get;}
public string B2 {set; get;}
public List<Foo> Foos {set; get;}
}
以下 linq 查询有错误,指出 foo
不包含名为 F1< 的属性/代码>。
var query = from foo in session.Linq<Foo>()
select foo.ContainerBar;
query = query.Where(foo => foo.F1 == "abcdef");
我知道第二个语句中的 foo
实际上是一个 Bar
因为查询选择了 ContainerBar
。
问题是知道如何在不更改原始查询的情况下添加动态 where 子句进行查询?最终目标是使用 linq-to-nhibernate 进行子查询。
Suppose I have classes Foo
and Bar
as follow:
public class Foo
{
public string F1 {set; get;}
public string F2 {set; get;}
public Bar ContainerBar {set; get;}
}
public class Bar
{
public string B1 {set; get;}
public string B2 {set; get;}
public List<Foo> Foos {set; get;}
}
Following linq query has errors saying that foo
does not contain a property named F1
.
var query = from foo in session.Linq<Foo>()
select foo.ContainerBar;
query = query.Where(foo => foo.F1 == "abcdef");
I know foo
in second statement is really a Bar
because query selects ContainerBar
.
The question is know how can I add a dynamic where clause to query without changing origianl query? Final goal is to have sub-queries with linq-to-nhibernate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的“查询”对象现在是 ContainerBar 的 IQueryAble
因此,当您执行Where( foo => foo.F1 == "abcdef" ) 时,它是在IQueryable 上完成的,因此没有F1 属性。
你应该这样做:
或者:
Your "query" object is now an IQueryAble of ContainerBar's
So when you do the Where( foo => foo.F1 == "abcdef" ), it's done on IQueryable, so no F1 property.
You should do:
Or:
您使用 NHibernate 3.0 吗?第一个查询对我不起作用(NHibernate 2.1.2.4000,无效转换)。但是,看起来您正在尝试获取所有具有 Foos 的 Bars,可以像这样完成...
现在您已经有了 Bars,在后面的代码中您可以像这样检查 F1...
Are you using NHibernate 3.0? The first query doesn't work for me (NHibernate 2.1.2.4000, invalid cast). However, it looks like you're trying to get all Bars that have Foos, which can be done like this...
Now that you have the Bars, in your later code you can check F1 like this...