如何使用Linq选择计数和最大值

时间:2013-09-15 14:55:13

标签: c# sql linq

SQL中的

我有以下语句,即日志文件中的错误实例。有人可以帮我把它转换成Linq吗?

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

1 个答案:

答案 0 :(得分:1)

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