动态linq-to-nhibernate查询问题

发布于 2024-10-02 01:18:36 字数 739 浏览 1 评论 0原文

假设我有类 FooBar 如下:

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 技术交流群。

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

发布评论

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

评论(2

丢了幸福的猪 2024-10-09 01:18:36
var query = from foo in session.Linq<Foo>()
                 select foo.ContainerBar;

query = query.Where(foo => foo.F1 == "abcdef");

您的“查询”对象现在是 ContainerBar 的 IQueryAble
因此,当您执行Where( foo => foo.F1 == "abcdef" ) 时,它是在IQueryable 上完成的,因此没有F1 属性。

你应该这样做:

var bars = from foo in session.Linq<Foo>()
            where foo.F1 == "abcdef"
            select foo.ContainerBar;

或者:

var q = session.Linq<Foo>();

// if some condition
q = q.Where( foo => foo.F1 == "abcdef" );

var bars = q.Select( foo => foo.ContainerBar );
var query = from foo in session.Linq<Foo>()
                 select foo.ContainerBar;

query = query.Where(foo => foo.F1 == "abcdef");

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:

var bars = from foo in session.Linq<Foo>()
            where foo.F1 == "abcdef"
            select foo.ContainerBar;

Or:

var q = session.Linq<Foo>();

// if some condition
q = q.Where( foo => foo.F1 == "abcdef" );

var bars = q.Select( foo => foo.ContainerBar );
趁微风不噪 2024-10-09 01:18:36

您使用 NHibernate 3.0 吗?第一个查询对我不起作用(NHibernate 2.1.2.4000,无效转换)。但是,看起来您正在尝试获取所有具有 Foos 的 Bars,可以像这样完成...

IQueryable<Bar> bars = Session
    .Linq<Bar>()
    .Where(bar => bar.Foos.Any());

现在您已经有了 Bars,在后面的代码中您可以像这样检查 F1...

var result = bars
    .Where(bar => bar.Foos.Any(foo => foo.F1 == "b"));

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...

IQueryable<Bar> bars = Session
    .Linq<Bar>()
    .Where(bar => bar.Foos.Any());

Now that you have the Bars, in your later code you can check F1 like this...

var result = bars
    .Where(bar => bar.Foos.Any(foo => foo.F1 == "b"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文