Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse a LINQ to SQL query

I want to reverse the output of a LINQ to SQL query. and .Reverse() does not work, just crashes the page.

protected void Page_Load(object sender, EventArgs e)
{
    NewsDataContext db = new NewsDataContext();
    var News = from news in db.News                   
               select new 
               {
                   ID = news.NewsID,
                   kort = news.Short
               };

    foreach (var newa in News)
    {
        Panel1.
        Controls.
        Add(new LiteralControl(newa.kort + 
                               "</br>" + 
                               "<a href=Full.aspx?id=" + 
                               newa.ID + 
                               ">Read The full Article</a>"));
    }
}

Is there another way to reverse it?

like image 700
Thomas Andreè Wang Avatar asked Nov 27 '22 22:11

Thomas Andreè Wang


1 Answers

If it's news you are trying to show, why not use an OrderBy (or OrderByDescending) to get the items in the order you want rather than the reverse order of the clustered index in the database.

I cannot think of any case where I would need to use Reverse in Linq to Sql.

Example (sorry if the Syntax if off, I prefer Lamda's):

var News = 
     (from news in db.News   
     orderby news.Date descending                 
     select new 
         {
               ID = news.NewsID,
               kort = news.Short
         });
like image 61
DaveShaw Avatar answered Nov 30 '22 10:11

DaveShaw