首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Linq C#中按[ColumnName]选择COUNT (ColumnName)组

在Linq C#中按[ColumnName]选择COUNT (ColumnName)组
EN

Stack Overflow用户
提问于 2016-06-24 15:36:18
回答 1查看 1.3K关注 0票数 0

我正在尝试使用C#对记录进行计数,并按Linq中的特定列对它们进行分组。下面是我想要转换为Linq的SQL语句。我希望将分组记录放到IEnumerable<object>List<object>中,以便将列表传递给我的图表。

代码语言:javascript
复制
SELECT 
G.[Description] AS Grade,
COUNT(CASE WHEN A.ApplicationStatusId = 1 THEN 1 END) AS Pending,
COUNT(CASE WHEN A.ApplicationStatusId = 2 THEN 1 END) AS Accepted,
COUNT(CASE WHEN A.ApplicationStatusId = 3 THEN 1 END) AS Rejected,
COUNT(ApplicationId) AS Total
FROM [Application] A
LEFT JOIN Grade G ON A.GradeId = G.GradeId

GROUP BY G.[Description]

这些是上述SQL语句的SQL结果。

我的课

代码语言:javascript
复制
public class Application : Audit
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ApplicationId { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime? ApplicationDate { get; set; }
        [StringLength(150)]
        public string PreviousSchoolName { get; set; }
        [StringLength(500)]
        public string PreviousSchoolReportUrl { get; set; }
        [StringLength(500)]
        public string PreviousSchoolTransferUrl { get; set; }
        public bool? Deleted { get; set; }

        #region Foreign Keys
        [ForeignKey("Leaner")]
        public int? LeanerId { get; set; }
        [ForeignKey("ApplicationStatus")]
        public int? ApplicationStatusId { get; set; }
        [ForeignKey("Grade")]
        public int? GradeId { get; set; }
        #endregion

        #region Navigation Properties
        public virtual Leaner Leaner { get; set; }
        public virtual ApplicationStatus ApplicationStatus { get; set; }
        public virtual Grade Grade { get; set; }
        #endregion

        public static Application GlobalApplication { get; set; }
        public static IEnumerable<Grade> Grades { get; set; }

        #region Dashboard Preperties - No Database Mapping
        [NotMapped]
        public int TotalApplications { get; set; }
        [NotMapped]
        public string GradeName { get; set; }
        #endregion
    }

public class Grade : LookupBase
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int GradeId { get; set; }


    }

public abstract class LookupBase
    {
        [StringLength(50)]
        public string Name { get; set; }
        [StringLength(250)]
        public string Description { get; set; }
    }

public abstract class Audit
    {
        [DataType(DataType.DateTime)]
        public DateTime? DateCreated { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime? DateModified { get; set; }
        public int? CreatedByOnlineUserId { get; set; }
        public int? ModifiedByOnlineUserId { get; set; }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-24 17:24:09

当SQL聚合函数COUNT(expr)不能被null (与A.ApplicationStatusId列相同)、Count(expr != null)Sum(expr != null ? 1 : 0)时映射到LINQ null(我更喜欢第二个,因为它似乎被EF翻译得更好)。

因此,等效的LINQ查询可以如下所示:

代码语言:javascript
复制
var query =
    from a in db.Application
    group a by a.Grade.Description into g
    select new
    {
        Grade = g.Key,
        Pending = g.Sum(a => a.ApplicationStatusId == 1 ? 1 : 0),
        Accepted = g.Sum(a => a.ApplicationStatusId == 2 ? 1 : 0),
        Rejected = g.Sum(a => a.ApplicationStatusId == 3 ? 1 : 0),
        Total = g.Count()
    };
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38017117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档