Using Linq expressions as a specification pattern with parent/child query

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

Answers


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.


Need Your Help

Test for overhead of virtual functions

c++ performance optimization virtual overhead

I set up a (perhaps very unscientific) small test to determine the overhead of virtual functions in a one-level single inheritance and the results I got were, well, exactly the same when accessing ...

javascript: when looping again through array, I want object property to reset

javascript angularjs loops object

I am trying to make an object, where the keys are number of years and value is amount of money