Get top 5 values with lambda query
You can use OrderBy()
to order the elements and then Take()
to take the first 5.
rptAnnouncement.DataSource = DbContext.Announcements
.Where(n => n.Expire_Date.Value.Date >= DateTime.Now.Date)
.OrderBy(n => n.Expire_Date.Value.Date)
.Take(5);
Notes
- You can order descending by using
OrderByDescending()
- Calling
ToList()
and then callingTake()
will get all of the items and then take the top 5 as opposed to only getting the top 5.
If you only want the Top 5 then you can use the below .
rptAnnouncement.DataSource = DbContext.Announcements.Where(n => n.Expire_Date.Value.Date >= DateTime.Now.Date).Take(5).ToList();
More details here
http://msdn.microsoft.com/en-us/library/bb503062.aspx