ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

在LINQ查询中使用GROUPBY时,无法转换LINQ表达式的GroupByShaperExpression:

2022-07-21 10:03:45  阅读:249  来源: 互联网

标签:set get Sum LINQ public GroupByShaperExpression TotalConfiguration GROUPBY Proje


我想对表中的每一列求和,如下所示:输入镜像描述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; }
    }

此表的模型称为项目如下所示:

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的object中。我的代码:

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对象)

 

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)
    };

 

 

转 https://cloud.tencent.com/developer/ask/sof/256693

标签:set,get,Sum,LINQ,public,GroupByShaperExpression,TotalConfiguration,GROUPBY,Proje
来源: https://www.cnblogs.com/wl-blog/p/16500840.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有