tags:

views:

63

answers:

3

Select Top(8) * from products order by CreatedOn desc can u convert this query to Linq List where product is a table in sql Created on is a dateTime comumn..

A: 

Check this StackOverflow question which talks about converting similar kind of query:

http://stackoverflow.com/questions/606124/converting-sql-containing-top-count-group-and-order-to-linq-2-entities

Also better work out on your accept rate too.

Sachin Shanbhag
@Timwi - Thanks. Did not realize it was a duplicate from stack overflow itself.
Sachin Shanbhag
+3  A: 
var result = (from p in products
             orderby p.CreatedOn descending
             select p).Take(8);

OR

var result = products.OrderByDescending(p=>p.CreatedOn).Take(8);
Danny Chen
Shouldn't it say Take(8)? :)
Marko
@Marko Ivanovski @Timwi - sorry a typo, thanks for your help.
Danny Chen
+1  A: 

Have a look at the Linq to Sql Cheat Sheet. It contains a lot of useful, easy to follow LinqToSql information and is available for C# and VB.NET.

Relating to your query, look at the Paging and Order section, taking out the Skip(x) part of the query, and and replace the .Take(5) with your .Take(8) for your Top(8) value.

Riaan