开发者

SQL to LINQ query asp.net

I am currently trying to get some statistics for my website but i cant seem to create the query 开发者_C百科for my database to get the username that if found most frequent in all the rows.

The sql query should look something like this:

SELECT username FROM Views GROUP BY 'username' ORDER BY COUNT(*) DESC LIMIT 1

How do i make that query in my controller?


var username = db.Views.GroupBy(v => v.username).OrderByDescending(g => g.Count()).First().Key


(from a in Views
group a by a.username into b
let c = b.count()
orderby c descending
select a.username).take(1);

Your query conversion .....


This is how you do that query using LINQ:

var temp = (from a in Views
            group a.username by a.username into b
            orderby b.Count() descending
            select b.Key).Take(1);


You can't do LIMIT 1 (mysql), since LinqToSql only generates TSql from MSSqlServer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜