SQL語句和EF Group by 用法


1,Group by 根據某個字段排序

       select Department,count(*) FROM [PPMG].[dbo].[UnConViolation] group by Department

結果如下:

2,Group by與所傳參數聯合使用

USE PPMG

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Alter PROCEDURE UnConViolationTimes 
    @type nvarchar(50)
AS
BEGIN
    declare @sql nvarchar(1000)
    if @type is not null
    begin
      set @sql='select '+ @type+' as Name,count(*) as Count FROM [PPMG].[dbo].[UnConViolation] group by '+@type
      print @sql
      exec(@sql)
    end    
END
GO

exec UnConViolationTimes 'Department'

結果如下

 

3,Group by 后台用法

public List<ViolationTimesDto> GetUnConViolationTimes(string type)
        {
            var UnConViolationList = UnConViolationRepository.Query();

            var ttList = UnConViolationList.GroupBy(m => new { m.Department}).Select(m => new ViolationTimesDto()
            {
                Name=m.Key.Department,
                Count=m.Count()
            }).ToList();            
            return ttList;
        }        

 結果如下

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM