Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ OrderBy not ordering .. changing nothing .. why?

Tags:

People also ask

Is LINQ OrderBy ascending?

In LINQ, the OrderBy operator is used to sort the list/ collection values in ascending order. In LINQ, if we use order by the operator by default, it will sort the list of values in ascending order. We don't need to add any ascending condition in the query statement.

What is difference between OrderBy and ThenBy in LINQ?

Generally, ThenBy method is used with the OrderBy method. The OrderBy() Method, first sort the elements of the sequence or collection in ascending order after that ThenBy() method is used to again sort the result of OrderBy() method in ascending order.

Does OrderBy work on string?

OrderBy" function utilizes the default comparer for a string. That comparer is not necessarily going to return a sort order based on the ASCII code. For a list of all the different string comparers, see the article on MSDN.


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