如何使用 Dynamic Linq 进行左外连接?
我试图模仿左外连接 此处 但使用动态 linq 扩展方法。我所拥有的:
public static IQueryable SelectMany(this IQueryable source, string selector,
string resultsSelector, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (selector == null) throw new ArgumentNullException("selector");
// Parse the lambda
LambdaExpression lambda = DynamicExpression.ParseLambda(
source.ElementType, null, selector, values);
// Fix lambda by recreating to be of correct Func<> type in case
// the expression parsed to something other than IEnumerable<T>.
// For instance, a expression evaluating to List<T> would result
// in a lambda of type Func<T, List<T>> when we need one of type
// an Func<T, IEnumerable<T> in order to call SelectMany().
Type inputType = source.Expression.Type.GetGenericArguments()[0];
Type resultType = lambda.Body.Type.GetGenericArguments()[0];
Type enumerableType = typeof(IEnumerable<>).MakeGenericType(resultType);
Type delegateType = typeof(Func<,>).MakeGenericType(inputType,
enumerableType);
lambda = Expression.Lambda(delegateType, lambda.Body, lambda.Parameters);
ParameterExpression[] parameters = new ParameterExpression[] {
Expression.Parameter(source.ElementType, "outer"),
Expression.Parameter(resultType, "inner")
};
LambdaExpression resultsSelectorLambda = DynamicExpression.ParseLambda(
parameters, null, resultsSelector, values);
// Create the new query
return source.Provider.CreateQuery(Expression.Call(typeof(Queryable),
"SelectMany", new Type[] {
source.ElementType,
resultType,
resultsSelectorLambda.Body.Type
}, source.Expression, Expression.Quote(lambda),
Expression.Quote(resultsSelectorLambda)));
}
和:
public static IQueryable GroupJoin(this IQueryable outer, IEnumerable inner,
string outerKeySelector, string innerKeySelector, string resultSelector,
params object[] values)
{
Type innerElementType = inner.AsQueryable().ElementType;
var outerParameter = Expression.Parameter(outer.ElementType, "outer");
var innerParameter = Expression.Parameter(innerElementType, "inner");
var groupParameter = Expression.Parameter(typeof(IEnumerable<>)
.MakeGenericType(innerElementType), "group");
var outerLambda = DynamicExpression.ParseLambda(new[] { outerParameter },
null, outerKeySelector, values);
var innerLambda = DynamicExpression.ParseLambda(new[] { innerParameter },
outerLambda.Body.Type, innerKeySelector, values);
var resultLambda = DynamicExpression.ParseLambda(new[] {
outerParameter, groupParameter }, null, resultSelector, values);
return outer.Provider.CreateQuery(Expression.Call(typeof(Queryable),
"GroupJoin", new[] { outer.ElementType, innerElementType,
outerLambda.Body.Type, resultLambda.Body.Type },
outer.Expression, Expression.Constant(inner),
Expression.Quote(outerLambda), Expression.Quote(innerLambda),
Expression.Quote(resultLambda)));
}
但是我失败的地方是 SelectMany
中的 DefaultIfEmpty
I am trying to mimick the left outer join here but using dynamic linq extension methods. What i have:
public static IQueryable SelectMany(this IQueryable source, string selector,
string resultsSelector, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (selector == null) throw new ArgumentNullException("selector");
// Parse the lambda
LambdaExpression lambda = DynamicExpression.ParseLambda(
source.ElementType, null, selector, values);
// Fix lambda by recreating to be of correct Func<> type in case
// the expression parsed to something other than IEnumerable<T>.
// For instance, a expression evaluating to List<T> would result
// in a lambda of type Func<T, List<T>> when we need one of type
// an Func<T, IEnumerable<T> in order to call SelectMany().
Type inputType = source.Expression.Type.GetGenericArguments()[0];
Type resultType = lambda.Body.Type.GetGenericArguments()[0];
Type enumerableType = typeof(IEnumerable<>).MakeGenericType(resultType);
Type delegateType = typeof(Func<,>).MakeGenericType(inputType,
enumerableType);
lambda = Expression.Lambda(delegateType, lambda.Body, lambda.Parameters);
ParameterExpression[] parameters = new ParameterExpression[] {
Expression.Parameter(source.ElementType, "outer"),
Expression.Parameter(resultType, "inner")
};
LambdaExpression resultsSelectorLambda = DynamicExpression.ParseLambda(
parameters, null, resultsSelector, values);
// Create the new query
return source.Provider.CreateQuery(Expression.Call(typeof(Queryable),
"SelectMany", new Type[] {
source.ElementType,
resultType,
resultsSelectorLambda.Body.Type
}, source.Expression, Expression.Quote(lambda),
Expression.Quote(resultsSelectorLambda)));
}
and:
public static IQueryable GroupJoin(this IQueryable outer, IEnumerable inner,
string outerKeySelector, string innerKeySelector, string resultSelector,
params object[] values)
{
Type innerElementType = inner.AsQueryable().ElementType;
var outerParameter = Expression.Parameter(outer.ElementType, "outer");
var innerParameter = Expression.Parameter(innerElementType, "inner");
var groupParameter = Expression.Parameter(typeof(IEnumerable<>)
.MakeGenericType(innerElementType), "group");
var outerLambda = DynamicExpression.ParseLambda(new[] { outerParameter },
null, outerKeySelector, values);
var innerLambda = DynamicExpression.ParseLambda(new[] { innerParameter },
outerLambda.Body.Type, innerKeySelector, values);
var resultLambda = DynamicExpression.ParseLambda(new[] {
outerParameter, groupParameter }, null, resultSelector, values);
return outer.Provider.CreateQuery(Expression.Call(typeof(Queryable),
"GroupJoin", new[] { outer.ElementType, innerElementType,
outerLambda.Body.Type, resultLambda.Body.Type },
outer.Expression, Expression.Constant(inner),
Expression.Quote(outerLambda), Expression.Quote(innerLambda),
Expression.Quote(resultLambda)));
}
However where I fall down is with the DefaultIfEmpty
within the SelectMany
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加
void DefaultIfEmpty();
到interface IEnumerableSignatures
然后使用
然后你有一个像这样的调用
Add
void DefaultIfEmpty();
tointerface IEnumerableSignatures
Then use
Then you have a call like