2017-08-05 140 views

回答

0

你可能应该通过LINQ的基础知识。微软文档有一整个章节专门为LINQ:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/

如果你在一个被称为类型的questions,列表数据List<Question>,那么你应该能够在您的查询转换是这样的:

var ret = from q in questions 
      group q by q.StepId into grouped 
      let count = grouped.Count() 
      orderby count 
      select new { StepId = grouped.Key, nb = count }; 
+0

(1)基团是一个'IGrouping <,>',所以得到从其'.Key'属性的键(2)基团识别单个分组,所以不要使用复数名称。 (3):不要Count()两次。 – tinudu

+0

感谢您的建议。 – sauravsahu

+0

非常感谢你的工作:D –

0

查询理解语法:在方法的语法

from q in questions 
group q by q.StepId into g 
select new { StepId = g.Key, Count = g.Count() } into stepCount 
orderby stepCount.Count 
select stepCount; 

完全一样(我比较喜欢的,因为它可以全部查询语法可以加更多,也往往是更紧凑):

questions 
.GroupBy(q => q.StepId) 
.Select(g => new { StepId = g.Key, Count = g.Count() }) 
.OrderBy(stepCount => stepCount.Count) 

使用另一种过载GroupBy变体:

questions 
.GroupBy(q => q.StepId, (key, values) => new { StepId = key, Count = values.Count() }) 
.OrderBy(stepCount => stepCount.Count); 
+0

非常感谢你的工作:D –

+0

如果你提出了答案并可能将其标记为已接受(或其他答案,因为它大致相同),会很高兴。狩猎声誉... :-) – tinudu