//先做基本查询 var querySql = from l in _logClinicDataOperationRepository.Table select new LogClinicDataOperationInfo { LogSeq = l.LogSeq, TypeCode = l.TypeCode, CreateDate = l.CreateDate }; //条件在基本的查询中完成 if (query.CreateDateStart != null) { querySql = querySql.Where(t => t.CreateDate >= query.CreateDateStart); } if (query.CreateDateEnd != null) { querySql = querySql.Where(t => t.CreateDate <= query.CreateDateEnd); } //然后把基本的条件过滤后的查询语句去分组 var ret = querySql.GroupBy(t => new { t.TypeCode }).Select(s => new LogClinicDataOperationInfo { //分组后的数据如果想去count,就用托管变量s去调用count的方法,不是querySql去掉用 LogSeq = s.Sum(m => m.LogSeq), //这里显示的是普通的字段,注意的是这里的s.Key如果分组字段只有一个的话,可以只写s.Key,如果分组字段有多个的话,语句如下: //var ret = querySql.GroupBy(t => new { t.TypeCode,t.ClientKind }).Select(s => new LogClinicDataOperationInfo //{ // // LogSeq = s.Sum(m => m.LogSeq), // ClientKind=s.Key.ClientKind // TypeCode = s.Key.TypeCode //}); TypeCode = s.Key.TypeCode });