【问题标题】:InvalidOperationException: The LINQ expression for groupbyInvalidOperationException:groupby 的 LINQ 表达式
【发布时间】:2020-08-11 18:51:54
【问题描述】:

我正在实施一个 asp.net 核心项目。我在我的代码中写了如下查询,

var RegisteredReqStatus = (from t1 in _context.Apiapplicant
                                       join t2 in _context.ApiApplicantHistory on t1.Id equals t2.ApiApplicantId
                                       join t3 in _context.EntityType on t2.LastReqStatus equals t3.Id
                                       where t1.IsDeleted.Equals(false) && t1.LastRequestStatus == t2.Id
                                       group t2 by t2.LastReqStatus into ApiAppGp
                                       select new
                                       {
                                           lastReqName = ApiAppGp.FirstOrDefault().LastReqStatusNavigation.Name,
                                           ReqCount = ApiAppGp.Count()
                                       }).ToList();

但运行我的项目后,它显示如下错误:

InvalidOperationException:LINQ 表达式 '(GroupByShaperExpression: KeySelector: (a.lastReqStatus), ElementSelector:(EntityShaperExpression: EntityType: ApiApplicantHistory 值缓冲区表达式: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: False ) ) .FirstOrDefault()' 无法翻译。以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。请参阅https://go.microsoft.com/fwlink/?linkid=2101038 了解更多信息。

这是我的数据模型:

public partial class ApiApplicantHistory
    {
        public int Id { get; set; }
        public int? SentType { get; set; }
        public int? Reason { get; set; }
        public int? LastReqStatus { get; set; }


        public virtual Apiapplicant ApiApp { get; set; }
        public virtual EntityType LastReqStatusNavigation { get; set; }
        public virtual EntityType SentTypeNavigation { get; set; }
        public virtual EntityType ReasonNavigation { get; set; }
    }

 public partial class EntityType
    {
        public EntityType()
        {
ApiApplicantHistoryLastReqStatusNavigation = new HashSet<ApiApplicantHistory>();            ApiApplicantHistorySentTypeNavigation = new HashSet<ApiApplicantHistory>();
            ApiApplicantHistoryReasonNavigation = new HashSet<ApiApplicantHistory>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string EntityKey { get; set; }

public virtual ICollection<ApiApplicantHistory> ApiApplicantHistoryLastReqStatusNavigation { get; set; }        public virtual ICollection<ApiApplicantHistory> ApiAppHistorySentTypeNavigation { get; set; }
        public virtual ICollection<ApiApplicantHistory> ApiAppHistoryReasonNavigation { get; set; }

    }
}

public partial class Apiapplicant
    {
        public Apiapplicant()
        {
            ApiApplicantHistory = new HashSet<ApiApplicantHistory>();
        }

        public int Id { get; set; }
public bool? IsDeleted { get; set; }

        public int? LastRequestStatus { get; set; }

        public virtual ICollection<ApiApplicantHistory> ApiApplicantHistory { get; set; }
    }

如果有人可以建议我解决方案,我将不胜感激。

【问题讨论】:

  • 这个怎么样? “要么以可翻译的形式重写查询,要么通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估”
  • 对不起,我没明白你的意思
  • 哪位不理解语句“要么以可翻译的形式重写查询,要么通过插入对 AsEnumerable()、AsAsyncEnumerable() 的调用显式切换到客户端评估, ToList() 还是 ToListAsync()"?
  • 我不明白我的查询有什么问题。对我来说似乎是正确的
  • 仅仅因为它编译并不意味着它是正确的。实体框架必须将 C# LINQ 转换为 SQL 语句调用。您的代码无法翻译。该错误告诉您.FirstOrDefault() 有问题。您必须重写查询以便可以翻译它,或者将数据带入内存,以便您可以针对它运行标准 C# 查询。这有帮助吗?

标签: linq asp.net-core


【解决方案1】:

如果解决了请告诉我:

var RegisteredReqStatus =
(
    from t1 in _context.Apiapplicant
    join t2 in _context.ApiApplicantHistory on new { X = t1.Id, Y = t1.LastRequestStatus } equals new { X = t2.ApiApplicantId, Y = t2.Id }
    join t3 in _context.EntityType on t2.LastReqStatus equals t3.Id
    where !t1.IsDeleted
    group t2 by t2.LastReqStatus into ApiAppGp
    from x in ApiAppGp.Take(1)
    select new
    {
        lastReqName = x.LastReqStatusNavigation.Name,
        ReqCount = ApiAppGp.Count()
    }
).ToList();

【讨论】:

  • 非常感谢您的建议。现在对于“join t2 in _context.ApiApplicantHistory ....”行,它向我显示如下错误:join 子句中的表达式之一不正确。加入调用中的类型推断失败。
  • 能否请您发布您的对象模型?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-22
  • 2018-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多