Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable OrderBy [closed]

I'm using IEnumerable orderby to sort my items in ascending format but it does not work my query is like this:

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

steps = steps.OrderBy(step => step.X);

its not deffer to use OrderBy or OrderByDescending

why?

I want to use Sum() method to sum up some items and item orders is important (there are some rules)

I read in MSDN that should be enumerated to work but whats the good way (i didn't try it).

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

like image 975
Saeed Amiri Avatar asked Sep 27 '10 11:09

Saeed Amiri


2 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" pointed out in his comment, if X isn't primitive or doesn't implement IComparable or IComparable<TypeOfX>then you're not going to be able to order your list, with or without LINQ.

like image 62
Cᴏʀʏ Avatar answered Sep 22 '22 12:09

Cᴏʀʏ


This just 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());
}
like image 20
Oliver Avatar answered Sep 26 '22 12:09

Oliver