c# - 如何反转 LINQ to SQL 查询

标签 c# asp.net linq-to-sql

我想反转 LINQ to SQL 查询的输出。 .Reverse() 不起作用,只会使页面崩溃。

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>"));
    }
}

还有其他方法可以逆转吗?

最佳答案

如果您要显示的是新闻,为什么不使用 OrderBy(或 OrderByDescending)按您想要的顺序获取项目,而不是使用数据库中聚集索引的相反顺序。

我想不出任何需要在 Linq to Sql 中使用 Reverse 的情况。

示例(对不起,如果语法关闭,我更喜欢 Lamda 的):

var News = 
     (from news in db.News   
     orderby news.Date descending                 
     select new 
         {
               ID = news.NewsID,
               kort = news.Short
         });

关于c# - 如何反转 LINQ to SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6417380/

相关文章:

c# - 从列表而不是整个索引中删除对象属性

c# - 从图像创建半透明光标

c# - WCF 错误 : 'It is likely that certificate ' my cert' may not have a private key that is capable of key exchange

.net - 选择数据访问技术时要考虑的问题?

linq - 定期 InvalidCastException 和 "The server failed to resume the transaction"与 Linq

c# - 在 Fluent Migrator 中回滚到以前的版本

javascript - 将 aspx 页面加载到 <ext :Panel>

c# - 如何在未选中复选框的情况下按删除按钮时在客户端显示错误消息

css - ASP 编辑 GridView 样式

.net - 使用编译查询时我们还需要存储过程吗?