LINQ OrderBy不订购..什么都没改..为什么? [英] LINQ OrderBy not ordering .. changing nothing .. why?

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

问题描述

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

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

首先我自己的类型



First my own type

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

假设 / code>值是正确的。此外,请确保 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天全站免登陆