2016-06-24 86 views
0

我正在計數記錄,並使用C#在Linq中的特定列進行分組。 這是我想要轉換爲Linq的SQL語句。我想將分組記錄放到IEnumerable<object>List<object>,這樣我就可以將列表傳遞給我的圖表。SELECT COUNT(ColumnName)Group By Linq C#中的[ColumnName]#

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

enter image description here

我的課表

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

的可能的複製[LINQ到SQL - GROUP BY和計數(http://stackoverflow.com/questions/17757753/linq-to -sql-group-by-count) – MethodMan

+0

你有什麼問題?我在這裏沒有看到一個問題 – JSON

+0

您是否嘗試過使用MyTable.GroupBy(x => x.ColumnName))。Count()'? 「MyTable」是你的表,「ColumnName」是你想要分組的屬性/列嗎? –

回答

0

的SQL聚合函數COUNT(expr)被映射到LINQ Count()expr不能null(這是你的A.ApplicationStatusId列的情況下),Count(expr != null)Sum(expr != null ? 1 : 0)否則(我更喜歡第二個,因爲它似乎被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() 
    }; 
+1

非常感謝您的幫助。它按我想要的方式工作。非常感謝你@Ivan Stoev – Linda