LINQ OrderBy 未排序 .. 什么都没改变 .. 为什么? [英] LINQ OrderBy not ordering .. changing nothing .. why?

查看:28
本文介绍了LINQ OrderBy 未排序 .. 什么都没改变 .. 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

知道为什么 LINQ OrderBy 在以下代码中不起作用,(没有错误但方法没有排序......)

Any idea why the LINQ OrderBy is not working in following code, (have no errors but method does not sort ...)

首先是我自己的类型

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

然后查询:

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

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

Untill here all fine :-), but then

newView.OrderBy(x => x.viewed);

什么都没改变,...页面正在加载结果显示...但没有排序...嗅探

just changes nothing,... Page is loading results showing ... but no ordering ... sniff

我尝试使用(创建比较器对象...):

i have Try with (creating a comparer object ... ):

newView.OrderBy(x => (Int32)x.viewed, new CompareIntegers());

相同的结果,没有排序......

same result, no ordering ...

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

I do have workarounds but just wondering what is missing ....

任何建议将不胜感激,非常感谢:-)

Any suggestions will be appreciated thanks a lot :-)

推荐答案

不要丢弃返回值.OrderBy 扩展方法不会改变输入.试试:

Don't throw away the return value. The OrderBy extension method is does not mutate the input. Try:

newView = newView.OrderBy(x => x.viewed);

假设 viewed 值是正确的,没有理由不这样做.另外,请确保 OrderBy 在任何会破坏排序的操作(例如 Distinct)之后.

There is no reason why that won't work, assuming the viewed value is correct. Also, make sure that OrderBy is after any operations (e.g. Distinct) which will ruin ordering.

快乐编码!

这篇关于LINQ OrderBy 未排序 .. 什么都没改变 .. 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆