LINQ OrderBy没有订购..改变什么..为什么?

时间:2021-09-17 22:49:46

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

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

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

then query :

然后查询:

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

非常感谢任何建议:-)

1 个解决方案

#1


32  

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

不要丢掉返回值。 OrderBy扩展方法不会改变输入。尝试:

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

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.

假设查看的值是正确的,没有理由不这样做。此外,确保OrderBy在任何操作(例如Distinct)之后,这将破坏排序。

Happy coding!

快乐的编码!

#1


32  

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

不要丢掉返回值。 OrderBy扩展方法不会改变输入。尝试:

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

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.

假设查看的值是正确的,没有理由不这样做。此外,确保OrderBy在任何操作(例如Distinct)之后,这将破坏排序。

Happy coding!

快乐的编码!