LINQPad query does not return result when using GroupBy and First or FirstOrDefault (semicolon)

49 Views Asked by At

In LINQPad 7 a relatively simple GroupBy query returns results but when you add First() or FirstOrDefault() - the result is not returned.

(Processes is the table I am targeting in this example but I don't think that is the cause for the problem.)

Results

Processes
    .GroupBy(p => p.CreatedDate)
    .OrderByDescending(x => x.Key)
    .Select(g => new { CreatedDate = g.Key, Count = g.Count() })

enter image description here

No Result

Processes
    .GroupBy(p => p.CreatedDate)
    .OrderByDescending(x => x.Key)
    .Select(g => new { CreatedDate = g.Key, Count = g.Count() })
    .First();

enter image description here

1

There are 1 best solutions below

1
Paul C On

So the answer turned out to be simple. Change to Language: 'C# Expression' and DO NOT include a semicolon when using just a query (expression). This then returned the expected result This Answer on SO - helped me https://stackoverflow.com/a/8011393/427684

enter image description here

If you want full valid C# with a semicolon then change to Language 'C# Statement' - store the result in a variable and then use LINQPad extension method .Dump()

enter image description here