【问题标题】:SELECT COUNT (ColumnName) Group By [ColumnName] in Linq C#SELECT COUNT (ColumnName) Group By [ColumnName] in Linq C#
【发布时间】:2016-06-24 15:36:18
【问题描述】:

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

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 结果。

我的课

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

【问题讨论】:

  • 你有什么问题?我在这里没有看到问题
  • 你试过MyTable.GroupBy(x =&gt; x.ColumnName)).Count()吗? MyTable 是您的表格,ColumnName 是您要分组的属性/列?
  • 我想在 Linq 中实现这个 SQL 查询,因为我首先使用 MVC 实体框架代码。所以我不能在我的 MVC 应用程序中编写 SQL 查询。
  • 所以我想在我的控制器上编写 Linq 语句并将结果传递给我的视图

标签: c# linq entity-framework-core


【解决方案1】:

expr 不能是null(您的A.ApplicationStatusId 列就是这种情况)、Count(expr != null)Sum(expr != null ? 1 : 0) 时,SQL 聚合函数 COUNT(expr) 映射到 LINQ Count(),否则(我更喜欢第二个,因为它似乎被 EF 翻译得更好)。

所以等效的 LINQ 查询可能是这样的:

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

【讨论】:

  • 非常感谢您的帮助。它完全按照我想要的方式工作。非常感谢@Ivan Stoev
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 2012-12-08
  • 1970-01-01
  • 2011-09-15
  • 2016-06-28
相关资源
最近更新 更多