我正在尝试使用作为 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;
}
}