stuck stuck - 6 months ago 36
SQL Question

How to select count and max using Linq

in SQL I have the following statement that instances of errors in log files. Can someone help me convert this into Linq?

select
*
from
(
select
distinct
bucket,
count(bucket) as count,
max(error) as error
from
logs
group by
bucket
) a
order by
count desc

Answer
var query = logs
    .GroupBy(x=>x.bucket)
    .Select
    (x=>
        new
        {
            bucket = x.Key,
            count = x.Count(z=>z),
            error = x.Max(z=>z.error)
        }
    )
    .OrderByDescenting(x=>x.count);
Comments