简体   繁体   English

LINQ Lambda表达式不起作用

[英]The LINQ lambda expression not working

I have the below Linq expression 我有下面的Linq表达式

var orderByLambda = Expression.Lambda<Func<Queue, int>>(
    nullCheckExpression, parameterExpression);

queue = context.Table
    .Join(context.tablea, cq => cq.a, r => r.a, (cq, r) => new { cq, r })
    .Join(context.tableb, s => s.r.b, se => se.b, (s, se) => new { s, se })
    .Join(context.tablec, u => u.s.cq.c, us => us.c, (u, us) => new { u, us })
    .Where(cq => cq.u.s.cq.c == Utilities.Authentication.c)
    .Where(cq => buildStatusOrder.Contains((BuildStatusEnum)cq.u.s.cq.d))
    .OrderBy(o => o.u.se.b)
    .Select(s => new QueueInfo
    {
        x = s.u.c,
        y = s.u.d,
        z = s.u.a
    });

queue = queue.OrderBy(f => orderByLambda);

var concat = queue.GroupBy(e => new { e.x, e.y, e.z })
    .OrderBy(v => v.FirstOrDefault().segmentID)
    .ToList()
    .Select(ss => new QueueInfo
    {
        x = ss.x,
        y = ss.y,
        z = ss.z,
    })
    .AsQueryable();

I am getting below error in concat 我在concat遇到错误

The LINQ expression node type 'Lambda' is not supported in LINQ to Entities.

What went wrong in my code? 我的代码出了什么问题?

Instead of 代替

queue = queue.OrderBy(f => orderByLambda);

Use: 采用:

queue = queue.OrderBy(orderByLambda);

Two notes on your code: 关于代码的两个注意事项:

  1. LINQ to Entities (Entity Framework) wants to translate your query into SQL, so it can only do operations that it knows how to translate. LINQ to Entities(实体框架)希望将查询转换为SQL,因此它只能执行知道如何转换的操作。 For example, you can't use most common LINQ collection methods such as Contains , etc. 例如,您不能使用最常见的LINQ收集方法,例如Contains等。
  2. You have defined a lambda object, but not what the actual expression does , to use for sorting - or at least, you haven't shown us what nullCheckExpression and parameterExpression are. 您已经定义了一个拉姆达对象,但实际表现没有什么,用于排序-或者至少,你还没有告诉我们什么nullCheckExpressionparameterExpression是。 In general, this is not how you would sort LINQ to Entities anyway - it should be something like queue.OrderBy(f => fx).ThenBy(f => fy); 总的来说,这不是您将LINQ排序为实体的方式-应该类似于queue.OrderBy(f => fx).ThenBy(f => fy); - you have to define which fields from your select are actually used for the sort. -您必须定义选择中的哪些字段实际用于排序。 (See point number 1) (请参阅第1点)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM