var group = excuses.GroupBy(m => m.Reason) .OrderByDescending(m => m.Count()) .Select(m => m.Key) .FirstOrDefault();
What creates the following sql statement:
SELECT TOP (1) [t1].[Reason] FROM ( SELECT COUNT(*) AS [value], [t0].[Reason] From [dbo].[Excuses] As [t0] GROUP BY [t0].[Reason] ) As [t1] ORDER BY [t1].[value] DESC
Since this is a moderately complex IQueryable expression, you might consider compiling it to speed up the response time:
Func<ExcusesDataContext, string> commonResult = CompiledQuery.Compile( (ExcusesDataContext c) => c.Excuses.GroupBy(m => m.Reason).OrderByDescending(m => m.Count()).Select(m => m.Key).FirstOrDefault() ); Console.WriteLine(commonResult(new ExcusesDataContext())); Console.ReadLine();
You can also just call the stored procedure through the repository and hook the specific value you are looking for. This would be the fastest path to happiness, but the least pleasure to maintain:
string excuse = this.repo.Excuses.MostCommonFor(ProblemList.BeingLate);
source share