Jan 9, 2009

LINQ

LINQ

http://www.pin5i.com/showtopic-16798.html

First you have to get the twenty items with the highest number, then you have
to order the list again.

A test I made:
var Tags = new[] {
new { Name = "test1", Count=3 },
new { Name = "test2", Count=7 },
new { Name = "test3", Count=45 },
new { Name = "test4", Count=1 },
new { Name = "test5", Count=6 },
new { Name = "test6", Count=34 },
new { Name = "test7", Count=78 },
new { Name = "test8", Count=32 },
new { Name = "test9", Count=54 },
new { Name = "test10", Count=67 },
new { Name = "test11", Count=32 },
new { Name = "test12", Count=63 },
new { Name = "test13", Count=13 },
new { Name = "test14", Count=86 },
new { Name = "test15", Count=23 },
new { Name = "test16", Count=36 },
new { Name = "test17", Count=26 },
new { Name = "test18", Count=67 },
}.AsQueryable();

var result = (from t in Tags
orderby t.Count descending
select t).Take(10).OrderBy(t => t.Name);

This one just takes 10 since I didn't want to make that many object.

So your thing would be someting like:


viewData.TagsPapers = (from t in database.Tags
orderby t.Count descending
select t).Take(20).OrderBy(t => t.Name);

If you are using LINQ to SQL it will create SQL similar to this
SELECT <fields>
FROM (
SELECT <fields>
FROM Tags
ORDER BY Count DESC
)
ORDER BY Name
--
Glenn F. Henriksen
http://www.henriksen.no/

No comments: