user3012037
user3012037

Reputation: 23

Refactoring LINQ to Query Expressions

How can I refactor this LINQ to a Query Expression ??

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

Upvotes: 0

Views: 49

Answers (1)

clhereistian
clhereistian

Reputation: 1300

Is this what you're looking for?

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

Upvotes: 1

Related Questions