LINQ OrderBy不订购..一无所获..为什么?

use*_*838 20 linq entity-framework iqueryable sql-order-by

知道为什么LINQ OrderBy不能在下面的代码中工作,(没有错误,但方法不排序......)

首先是我自己的类型

public class IQLinksView
    {
        public int id { get; set; }
        public int catid { get; set; }
        public int? viewed {get;set;}
        public string name {get;set;}
        public string desc {get;set;}
        public string url {get;set;}
        public string pic {get;set;}
        public string cat {get;set;}
    }
Run Code Online (Sandbox Code Playgroud)

然后查询:

IQueryable<IQLinksView> newView = 
              from links in this.emContext.tbl_otherlinks
              select new IQLinksView { id = links.pklinkid, catid =
              links.tbl_catgeory.pkcategoryid, viewed = links.linkviewed, name = links.linkname, 
              desc = links.linkdesc, pic = links.linkpicture,   url = links.linkurl, cat =
              links.tbl_catgeory.categoryname };
Run Code Online (Sandbox Code Playgroud)

直到这里一切都很好:-),但随后

newView.OrderBy(x => x.viewed);
Run Code Online (Sandbox Code Playgroud)

只是改变什么,...页面正在加载结果显示...但没有订购...嗅探

我试过(创建一个比较对象......):

newView.OrderBy(x => (Int32)x.viewed, new CompareIntegers());
Run Code Online (Sandbox Code Playgroud)

同样的结果,没有订购......

我确实有解决方法,但只是想知道缺少什么....

非常感谢任何建议:-)

小智 39

不要丢掉返回值.的OrderBy延伸方法是不发生变异的输入.尝试:

newView = newView.OrderBy(x => x.viewed);
Run Code Online (Sandbox Code Playgroud)

假设viewed值正确,没有理由不这样做.此外,确保OrderBy在任何操作(例如Distinct)之后,这将破坏订购.

快乐的编码!