IEnumerable OrderBy

I use IEnumerable orderby to sort my items in an upstream format, but it doesn’t work, my query looks like this:

IEnumerable<Step> steps = allsteps.Where(step => step.X <= Y); steps = steps.OrderBy(step => step.X); 

it should not be used OrderBy or OrderByDescending

why?

I want to use the Sum() method to summarize some elements and the order of the elements is important (there are some rules)

I read on MSDN which needs to be listed in order to work, but what good (I have not tried).

EDIT: X and Y are of type double . I checked the first element of my steps (steps. First ()) in quick view mode.

+7
c # linq sql-order-by ienumerable
source share
3 answers

First of all, why not just keep it on one line.

 var steps = allsteps.Where(step => step.X <= Y).OrderBy(step => step.X); 

As vc 74 noted in his comment, if X not primitive or does not implement IComparable or IComparable<TypeOfX> , then you will not be able to order your list with or without LINQ.

+8
source share

This works as expected:

 // Create some random double values from 0 - 100 var values = Enumerable.Repeat(new Random(), int.MaxValue) .Select(r => r.NextDouble() * 100); //Create an enumeration with ten elements var pointList = values.Take(10) //Cause of lacking support of IEnumerable.Zip() or .Pairwise() //use this approach to create something with the properties X and Y .Select(n => new PointF((float)n, (float)values.First())); //Sort the elements var sortedPoints = pointList.OrderBy(point => point.X); //Output to console foreach (var point in sortedPoints) { Console.WriteLine(point.ToString()); } 
+3
source share

Let's move on to a very similar problem with this:

 IEnumerable<Article> articles = StepThree.ReturnArticles(iUserID); lvPending.DataSource = articles; lvPending.DataBind(); 

I need to change the order in which the results will be tied to my list, so I tried this:

 lvPending.DataSource = articles.OrderByDescending(a=> a.DateCreated); 

He performed, but did not update my results. Therefore, looking further, I found that since I am returning a complex type, linq cannot order it for me, so I had to add the orderby command in descending order in the StepThree.cs file, where the ReturnArticles method is called.

+3
source share

All Articles