Increasing Group By performance in LINQ queries
In this section, we will look at three different ways of performing the same Group By operation. Each way provides a different performance level. You will see by the end of this section which method is best for performing fast Group By queries. The methods that we add in this section will be added to the LinqPerformance class.
For our scenario, we want to get a list of people from a collection that all share the same name. To extract those people, we will perform a Group By operation. Then, we will extract all those for whom the group count is greater than one, and then add them to a list of people.
Let us add our three methods that use the GroupBy clause to return a list of people:
- Add the
GroupByVersion1()method:[Benchmark] public void GroupByVersion1() { List<Person> People = _people.GroupBy(x => x.LastName) Â Â Â Â Â Â Â Â Â Â Â Â Â Â .Where(x => x...