如何实现外连接表达式树?
我需要使用表达式语法来实现查询(因为我不知道编译时的类型)。 例如这样的查询:
from customer in Customers
join purchase in Purchases
on customer.ID equals purchase.CustomerID
into outerJoin
from range in outerJoin.DefaultIfEmpty()
where
customer.Name == "SomeName" &&
range.Description.Contains("SomeString") &&
customer.ID == range.CustomerID
select
new { Customer = customer, Purchase = range }
我找到了实现这样的组连接部分的方法:
ITable p = _dataContext.GetTable(typeof(Purchases));
ITable c = _dataContext.GetTable(typeof(Customers));
LambdaExpression outerSelectorLambda = DynamicExpression.ParseLambda(typeof(Customers), null, "ID");
LambdaExpression innerSelectorLambda = DynamicExpression.ParseLambda(typeof(Purchases), null, "CustomerID");
ParameterExpression param1 = Expression.Parameter(typeof(Customers), "customer");
ParameterExpression param2 = Expression.Parameter(typeof(IEnumerable<Purchases>), "purchases");
ParameterExpression[] parameters = new ParameterExpression[] { param1, param2 };
LambdaExpression resultsSelectorLambda = DynamicExpression.ParseLambda(parameters, null, "New(customers as customers, purchases as purchases)");
MethodCallExpression joinCall =
Expression.Call(
typeof(Queryable),
"GroupJoin",
new Type[] {
typeof(Customers),
typeof(Purchases),
outerSelectorLambda.Body.Type,
resultsSelectorLambda.Body.Type
},
c.Expression,
p.Expression,
Expression.Quote(outerSelectorLambda),
Expression.Quote(innerSelectorLambda),
Expression.Quote(resultsSelectorLambda)
);
但我不知道如何使用这种语法编写查询的其余部分。 有人可以帮助我吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将遵循一种方法来实现这一目标:
获取 LINQ 查询的等价表达式。
获取从 LINQ 查询中提取的表达式的 ToString()。
研究表达式,了解输入参数、类型参数、表达式参数等...
如果有,请回复我所提到的方法尚不清楚。
I would follow an approach to achieve that:
Get the Expression equivalent of the LINQ query.
Get the ToString() of the Expression extracted from the LINQ query.
Study the Expression, to understand the input parameters, type parameters, Expression Arguments etc...
Get back to me if the approach mentioned is not clear.
我刚刚在dynamic.cs 中复制并粘贴了“join”实现,并进行了一些更改以使“GroupJoin”正常工作。
I just copied + pasted the "join" implementation in dynamic.cs and made couple of changes to make "GroupJoin" to work.