使用 Linq 表达式作为父/子查询的规范模式

发布于 2024-09-07 11:19:59 字数 1501 浏览 8 评论 0原文

我正在尝试使用作为 Linq 表达式实现的规范模式,以便 Linq 提供程序可以解析它以生成有效的数据库查询。

给出了基本的想法。

我很难尝试让它与父/子查询一起使用

class Parent
{
    public int Foo;

    public IList<Child> Children = new List<Child>();
}

class Child
{
    public int Bar;
}

class Program
{
    static void Main(string[] args)
    {
        IQueryable<Parent> qry = GetQry(); //initialised


        //This works but duplicates the IsBigBar() logic
        //Included to show what I am trying to query on
        var parentsWithBigChildBars =
                from parents in qry
                where parents.Children.Any(child => child.Bar > 10) 
                select parents;

        var parentsWithBigChildBars2 =
               from parents in qry
               where parents.Children.Any( ?? ) //but how do i access my IsBigBar() expression from here?
               select parents;
    }


    //I want to re-use it to pull parents back!
    public Expression<Func<Child, bool>> IsBigBar()
    {
        return child => child.Bar > 10;
    }

    //I'f i use this as the Any() delegate, it compiles & runs but not an expression so evaluated client side
    public Func<Child, bool> IsBigBar2()
    {
        return child => child.Bar > 10;
    }
}

I'm trying to use the specification pattern implemented as a Linq expression so that Linq providers can parse it to produce efficient database queries.

This gives the basic idea.

I am having a hard time trying trying to get it working with a parent/child query

class Parent
{
    public int Foo;

    public IList<Child> Children = new List<Child>();
}

class Child
{
    public int Bar;
}

class Program
{
    static void Main(string[] args)
    {
        IQueryable<Parent> qry = GetQry(); //initialised


        //This works but duplicates the IsBigBar() logic
        //Included to show what I am trying to query on
        var parentsWithBigChildBars =
                from parents in qry
                where parents.Children.Any(child => child.Bar > 10) 
                select parents;

        var parentsWithBigChildBars2 =
               from parents in qry
               where parents.Children.Any( ?? ) //but how do i access my IsBigBar() expression from here?
               select parents;
    }


    //I want to re-use it to pull parents back!
    public Expression<Func<Child, bool>> IsBigBar()
    {
        return child => child.Bar > 10;
    }

    //I'f i use this as the Any() delegate, it compiles & runs but not an expression so evaluated client side
    public Func<Child, bool> IsBigBar2()
    {
        return child => child.Bar > 10;
    }
}

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

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

发布评论

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

评论(1

谈场末日恋爱 2024-09-14 11:19:59

您想要:

    var predicate = IsBigBar();
    var parentsWithBigChildBars2 =
           from parents in qry
           where parents.Children.Any(predicate) 
           select parents;

额外的 var 非常重要。它可以防止查询提供程序(拥有 qry)尝试解释 IsBigBar( ) 并将其指向该方法的结果

You want:

    var predicate = IsBigBar();
    var parentsWithBigChildBars2 =
           from parents in qry
           where parents.Children.Any(predicate) 
           select parents;

The extra var is very important. It prevents the query provider (that owns qry) from trying to interpret IsBigBar() and instead points it at the result of that method.

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