views:

131

answers:

2

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'm want to use Sum() method to sum some items and item orders is important (there are some rules)

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

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

+3  A: 

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.

Cory Larson
yes its better to use this: var steps = allsteps.Where(step => step.X <= Y).OrderBy(step => step.X); thanks
SaeedAlg
A: 

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());
}
Oliver