C# 如何使用动态Linq进行左外连接?
我试图模仿左外连接,但使用动态linq扩展方法。我所拥有的:C# 如何使用动态Linq进行左外连接?,c#,.net,linq,linq-to-sql,dynamic-linq,C#,.net,Linq,Linq To Sql,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 ==
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 SelectMany(此IQueryable源、字符串选择器、,
字符串结果选择器,参数对象[]值)
{
如果(source==null)抛出新的ArgumentNullException(“source”);
如果(选择器==null)抛出新的ArgumentNullException(“选择器”);
//解析lambda
LambdaExpression lambda=DynamicExpression.ParseLambda(
source.ElementType,null,选择器,值);
//通过将lambda重新创建为正确的Func类型来修复
//解析为IEnumerable以外的内容的表达式。
//例如,将生成一个计算为List的表达式
//在Func类型的lambda中,当我们需要
//函数添加void DefaultIfEmpty();
到接口IEnumerableSignatures
然后使用
public static object DefaultIfEmpty(this IQueryable source)
{
if (source == null) throw new ArgumentNullException("source");
return source.Provider.Execute(
Expression.Call(
typeof(Queryable), "DefaultIfEmpty",
new Type[] { source.ElementType },
source.Expression));
}
那你有一个电话
var qry = Foo.GroupJoin(Bar, "outer.Id", "inner.Id", "new(outer.Id as Foo, group as Bars)").SelectMany("Bars.DefaultIfEmpty()", "new(outer.Foo as Foo, inner as Bar)");
您在这里指的是DynamicExpression的哪种实现?你们能提供更简要的信息吗?我需要使用这个:(
var qry = Foo.GroupJoin(Bar, "outer.Id", "inner.Id", "new(outer.Id as Foo, group as Bars)").SelectMany("Bars.DefaultIfEmpty()", "new(outer.Foo as Foo, inner as Bar)");