【问题标题】:The LINQ expression 'GroupByShaperExpression: could not be translated while using GROUPBY in LINQ queryLINQ 表达式 'GroupByShaperExpression: 在 LINQ 查询中使用 GROUPBY 时无法翻译
【发布时间】:2021-05-28 06:20:49
【问题描述】:

我想对表格中的每一列求和,如下所示: enter image description here

此表的模型称为 TotalConfiguration,如下所示:

public class TotalConfiguration
    {
        [Key]
        public int idTotalConfiguration { get; set; }
        [ForeignKey("Projects")]
        public Guid IdProjects { get; set; }
        public int January { get; set; }
        public int February { get; set; }
        public int March { get; set; }
        public int April { get; set; }
        public int May { get; set; }
        public int June { get; set; }
        public int July { get; set; }
        public int August { get; set; }
        public int September { get; set; }
        public int October { get; set; }
        public int November { get; set; }
        public int December { get; set; }
        public Projects Projects { get; set; }
    }

这个名为 Projects 的表的模型如下所示:

public class Projects
    {
        [Key]
        public Guid IdProject { get; set; }
        [Required]
        public string OwnerName { get; set; }
        [Required]
        public string ProjectAdress1 { get; set; }
        public string ProjectAdress2 { get; set; }
        [Required]
        public string ProjectPostcode { get; set; }
        [Required]
        public string ProjectCity { get; set; }
        public TotalConfiguration TotalConfiguration { get; set; }
}

这个名为 ProjectsUser 的表的模型如下所示:

 public class ProjectsUser
    { 
        [Key]
        public int IdProjectsUser { get; set; }
        [ForeignKey("User")]
        public Guid IdUser { get; set; }
        [ForeignKey("Projects")]
        public Guid IdProject { get; set; }
        [Required]
        public virtual User User { get; set;}
        [Required]
        public virtual Projects Projects { get; set; }
    }

在我的控制器中,我想将所有列与 TotalConfiguration 的对象相加。我的代码:

public IActionResult GetTotalConfiguration(Guid userId)
{
    TotalConfiguration totalConfiguration3 = _context.ProjectsUser
        .Where(x => x.IdUser == userId)
        .GroupBy(x => x.IdUser)
        .Select(x => new TotalConfiguration
        {
            January = x.Sum(x => x.Projects.TotalConfiguration.January),
            February = x.Sum(x => x.Projects.TotalConfiguration.January),
            March = x.Sum(x => x.Projects.TotalConfiguration.January),
            April = x.Sum(x => x.Projects.TotalConfiguration.January),
            May = x.Sum(x => x.Projects.TotalConfiguration.January),
            June = x.Sum(x => x.Projects.TotalConfiguration.January),
            July = x.Sum(x => x.Projects.TotalConfiguration.January),
            August = x.Sum(x => x.Projects.TotalConfiguration.January),
            September = x.Sum(x => x.Projects.TotalConfiguration.January),
            October = x.Sum(x => x.Projects.TotalConfiguration.January),
            November = x.Sum(x => x.Projects.TotalConfiguration.January),
            December = x.Sum(x => x.Projects.TotalConfiguration.January),
        })
        .FirstOrDefault();

如果我运行这段代码,我得到了错误:

System.InvalidOperationException: 'The LINQ expression 'GroupByShaperExpression:
KeySelector: p.IdUser, 
ElementSelector:EntityShaperExpression: 
    EntityType: ProjectsUser
    ValueBufferExpression: 
        ProjectionBindingExpression: EmptyProjectionMember
    IsNullable: False

    .Sum(x => x.Projects.TotalConfiguration.January)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'

当我将结果保存到 IEnumerable 时,我没有收到此错误(但我想将结果保存为 TotalConfiguration 对象)

【问题讨论】:

    标签: c# asp.net linq


    【解决方案1】:

    您不能在 GroupBy 之后使用导航属性。

    应该重写查询:

    var query =
        from pu in _context.ProjectsUser
        let tc = pu.TotalConfiguration
        group tc by pu.IdUser into g
        select new TotalConfiguration
        {
            idTotalConfiguration = g.Key,
            
            January = x.Sum(x => x.January),
            February = x.Sum(x => x.February),
            March = x.Sum(x => x.March),
            April = x.Sum(x => x.April),
            May = x.Sum(x => x.May),
            June = x.Sum(x => x.June),
            July = x.Sum(x => x.July),
            August = x.Sum(x => x.August),
            September = x.Sum(x => x.September),
            October = x.Sum(x => x.October),
            November = x.Sum(x => x.November),
            December = x.Sum(x => x.December)
        };
    

    【讨论】:

    • 非常非常感谢您的回答。我非常感谢您的帮助。我尝试使用您的代码,但出现很多错误(zapodaj.net/images/e89fcf0838fb0.png)。我尝试修复它并测试此代码(zapodaj.net/images/b9b388e8f9c82.png),但我遇到了与我的问题相同的错误。
    • 让我检查一下。从内存中写在浏览器中。
    • @AdrianWróbel,固定答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2022-12-03
    • 2021-07-26
    • 2021-05-23
    • 1970-01-01
    相关资源
    最近更新 更多