我想反转 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/