3
我試圖使用作爲Linq表達式實現的規範模式,以便Linq提供者可以解析它以生成高效的數據庫查詢。使用Linq表達式作爲具有父/子查詢的規範模式
This給出了基本思路。
我有一個艱難的時間,試圖設法得到它與父/子查詢
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;
}
}
hm,我得到無法投入'System.Linq.Expressions.ConstantExpression'類型的對象來鍵入'System.Linq.Expressions.LambdaExpression'。這是我的提供者中的錯誤嗎? – 2010-07-02 06:56:20
您的問題中的代碼是您正在運行的* actual *代碼嗎? – 2010-07-02 12:57:11