Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

entity framework - LINQ OrderBy not ordering .. changing nothing .. why?

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

Happy coding!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...