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.
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.
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With