将LINQ重构为查询表达式

时间:2014-04-05 01:58:54

标签: c# linq

如何将此LINQ重构为查询表达式?

    private static IEnumerable GetNameOccurrence(IEnumerable<string> allNames)
    {
        var nameTest = allNames.GroupBy(n => n).
            Select(group =>
                new
                {
                    Name = group.Key,
                    Count = group.Count()
                }).OrderByDescending(group => group.Count);

        return nameTest;
    }

1 个答案:

答案 0 :(得分:1)

这是你要找的吗?

 private static IEnumerable GetNameOccurrence(IEnumerable<string> allNames)
 {
    var nameTest = 
    (from name in allNames
    group name by name.n into nameGroup
    select new { Name = nameGroup.Key, Count = nameGroup.Count() })
    .OrderByDescending(nameGroup => nameGroup.Count);

    return nameTest;
}