LINQ 表达式树中 Where() 内部的 Any()。

7
我正在尝试生成以下LINQ查询:
//Query the database for all AdAccountAlerts that haven't had notifications sent out
//Then get the entity (AdAccount) the alert pertains to, and find all accounts that
//are subscribing to alerts on that entity.
var x = dataContext.Alerts.Where(a => a.NotificationsSent == null)
  .OfType<AdAccountAlert>()
  .ToList()
  .GroupJoin(dataContext.AlertSubscriptions,
    a => new Tuple<int, string>(a.AdAccountId, typeof(AdAccount).Name),
    s => new Tuple<int, string>(s.EntityId, s.EntityType),
    (Alert, Subscribers) => new Tuple<AdAccountAlert, IEnumerable<AlertSubscription>> (Alert, Subscribers))
  .Where(s => s.Item2.Any())
  .ToDictionary(kvp => (Alert)kvp.Item1, kvp => kvp.Item2.Select(s => s.Username));

使用表达式树(似乎这是我需要在需要使用反射和运行时类型时唯一的方法)。请注意,实际代码中(见下文),AdAccountAlert实际上是通过反射和for循环动态生成的。 我的问题:我可以生成除了.Where()子句之外的所有内容。whereExpression方法调用由于不兼容的类型而失败。通常我知道在那里放什么,但Any()方法调用让我感到困惑。我已经尝试了我能想到的每种类型,但都没有成功。希望对.Where()和.ToDictionary()两个方法都能得到帮助。
以下是我目前的代码:
var alertTypes = AppDomain.CurrentDomain.GetAssemblies()
  .Single(a => a.FullName.StartsWith("Alerts.Entities"))
  .GetTypes()
  .Where(t => typeof(Alert).IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface);

var alertSubscribers = new Dictionary<Alert, IEnumerable<string>>();

//Using tuples for joins to keep everything strongly-typed
var subscribableType = typeof(Tuple<int, string>);
var doubleTuple = Type.GetType("System.Tuple`2, mscorlib", true);

foreach (var alertType in alertTypes)
{
  Type foreignKeyType = GetForeignKeyType(alertType);
  if (foreignKeyType == null)
    continue;

  IQueryable<Alert> unnotifiedAlerts = dataContext.Alerts.Where(a => a.NotificationsSent == null);

  //Generates: .OfType<alertType>()
  MethodCallExpression alertsOfType = Expression.Call(typeof(Enumerable).GetMethod("OfType").MakeGenericMethod(alertType), unnotifiedAlerts.Expression);

  //Generates: .ToList(), which is required for joins on Tuples
  MethodCallExpression unnotifiedAlertsList = Expression.Call(typeof(Enumerable).GetMethod("ToList").MakeGenericMethod(alertType), alertsOfType);

  //Generates: a => new { a.{EntityId}, EntityType = typeof(AdAccount).Name }
  ParameterExpression alertParameter = Expression.Parameter(alertType, "a");
  MemberExpression adAccountId = Expression.Property(alertParameter, alertType.GetProperty(alertType.GetForeignKeyId()));
  NewExpression outerJoinObject = Expression.New(subscribableType.GetConstructor(new Type[] { typeof(int), typeof(string)}), adAccountId, Expression.Constant(foreignKeyType.Name));
  LambdaExpression outerSelector = Expression.Lambda(outerJoinObject, alertParameter);

  //Generates: s => new { s.EntityId, s.EntityType }
  Type alertSubscriptionType = typeof(AlertSubscription);
  ParameterExpression subscriptionParameter = Expression.Parameter(alertSubscriptionType, "s");
  MemberExpression entityId = Expression.Property(subscriptionParameter, alertSubscriptionType.GetProperty("EntityId"));
  MemberExpression entityType = Expression.Property(subscriptionParameter, alertSubscriptionType.GetProperty("EntityType"));
  NewExpression innerJoinObject = Expression.New(subscribableType.GetConstructor(new Type[] { typeof(int), typeof(string) }), entityId, entityType);
  LambdaExpression innerSelector = Expression.Lambda(innerJoinObject, subscriptionParameter);

  //Generates: (Alert, Subscribers) => new Tuple<Alert, IEnumerable<AlertSubscription>>(Alert, Subscribers)
  var joinResultType = doubleTuple.MakeGenericType(new Type[] { alertType, typeof(IEnumerable<AlertSubscription>) });
  ParameterExpression alertTupleParameter = Expression.Parameter(alertType, "Alert");
  ParameterExpression subscribersTupleParameter = Expression.Parameter(typeof(IEnumerable<AlertSubscription>), "Subscribers");
  NewExpression joinResultObject = Expression.New(
    joinResultType.GetConstructor(new Type[] { alertType, typeof(IEnumerable<AlertSubscription>) }),
    alertTupleParameter,
    subscribersTupleParameter);

  LambdaExpression resultsSelector = Expression.Lambda(joinResultObject, alertTupleParameter, subscribersTupleParameter);

  //Generates:
  //  .GroupJoin(dataContext.AlertSubscriptions,
  //    a => new { a.AdAccountId, typeof(AdAccount).Name },
  //    s => new { s.EntityId, s.EntityType },
  //    (Alert, Subscribers) => new Tuple<Alert, IEnumerable<AlertSubscription>>(Alert, Subscribers))
  IQueryable<AlertSubscription> alertSubscriptions = dataContext.AlertSubscriptions.AsQueryable();
  MethodCallExpression joinExpression = Expression.Call(typeof(Enumerable),
    "GroupJoin",
    new Type[]
    {
      alertType,
      alertSubscriptions.ElementType,
      outerSelector.Body.Type,
      resultsSelector.ReturnType
    },
    unnotifiedAlertsList,
    alertSubscriptions.Expression,
    outerSelector,
    innerSelector,
    resultsSelector);

  //Generates: .Where(s => s.Item2.Any())
  ParameterExpression subscribersParameter = Expression.Parameter(resultsSelector.ReturnType, "s");
  MemberExpression tupleSubscribers = Expression.Property(subscribersParameter, resultsSelector.ReturnType.GetProperty("Item2"));
  MethodCallExpression hasSubscribers = Expression.Call(typeof(Enumerable),
    "Any",
    new Type[] { alertSubscriptions.ElementType },
    tupleSubscribers);
  LambdaExpression whereLambda = Expression.Lambda(hasSubscribers, subscriptionParameter);
  MethodCallExpression whereExpression = Expression.Call(typeof(Enumerable),
    "Where",
    new Type[] { joinResultType },
    joinExpression,
    whereLambda);

19
只有一个问题:你认为你正在编写的代码易于阅读和维护吗? - MeTitus
3
真正的代码被拆分成几个单独的函数,这使得阅读变得更容易。为了提供上下文,我把所有东西都放在一起。如果你在询问我动态构建表达式树的使用方式,正如我在帖子中所说,这是目前我发现的最好的选择。PredicateBuilder无法完成任务,DynamicLinq库也无法做到。 - user1924929
2
我根本无法理解原始的LINQ首先要做什么,更不用说动态生成的LINQ了。 - Bobson
1
你当前的代码有什么问题?它是如何失败的?你尝试过比较你生成的表达式树和C#从lambda生成的表达式树吗? - svick
此外,如果您可以确认生成的查询在没有 .Where 子句的情况下是正确的(除了 .ToDictionary 调用中的 .Select 有时会返回空),那么您应该能够简单地确认所需的代码以生成等效于 Enumerable.Range(0,2).Select(i=>Enumerable.Range(0,i)).Where(r=>r.Any()) 的内容,然后添加一个“简单”的扩展,使 .Where 子句变为 r.item2.Any() - Mark Hurd
显示剩余7条评论
2个回答

3
请注意:从ToList()开始的所有内容都不适用于IQueryable<T>,而是适用于IEnumerable<T>。因此,无需创建表达式树。它肯定不是由EF或类似工具解释的内容。
如果您查看编译器为原始查询生成的代码,您会发现它仅在第一次调用ToList之前生成表达式树。
例如:
以下代码:
var query = new List<int>().AsQueryable();
query.Where(x => x > 0).ToList().FirstOrDefault(x => x > 10);

被编译器翻译为:

IQueryable<int> query = new List<int>().AsQueryable<int>();
IQueryable<int> arg_4D_0 = query;
ParameterExpression parameterExpression = Expression.Parameter(typeof(int), "x");
arg_4D_0.Where(Expression.Lambda<Func<int, bool>>(Expression.GreaterThan(parameterExpression, Expression.Constant(0, typeof(int))), new ParameterExpression[]
{
    parameterExpression
})).ToList<int>().FirstOrDefault((int x) => x > 10);

请注意它是如何为ToList之前的所有内容生成表达式的。之后以及包括它在内的所有内容都只是普通的扩展方法调用。
如果您在代码中不模仿这一点,实际上会向LINQ提供程序发送一个对Enumerable.ToList的调用 - 然后尝试将其转换为SQL并失败。

由于查询所依赖的类型在编译时不可知,因此需要动态构建表达式树。因此,无需创建表达式树。 - Mike Strobel
1
如果您的意思是OP需要在“ToList”之后动态构建东西,那当然可以这样做,但这不需要使用表达式树,而且可能不应该使用表达式树。在不需要表达式树的情况下省略它们会大大简化问题。 - user743382
如果编译时不知道类型和属性,他应该做什么呢? - Mike Strobel

0

看起来,在构造whereLambda时,你的第二个参数应该是subscribersParameter而不是subscriptionParameter。至少,这将是你异常的原因。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接