I probably worded this the wrong way, but I have a simple List that based on a split I want the greatest values from the first column.
Here is the code
List<string> myString = new List<string>();
myString.Add("100.02|apples|pears");
myString.Add("22.02|apples|pears");
myString.Add("99.02|apples|pears");
myString.Add("88.02|apples|pears");
myString.Add("77.02|apples|pears");
myString.Add("66.02|apples|pears");
I'm basically splitting on | and then converting the first column to decimal. I want to get the greatest 5 rows to this would exclude 22.02. I've tried sorting and ordering but they dont work because the strings are differnt lengths.
Thanks!
This will work:
List<string> myString = new List<string>();
myString.Add("100.02|apples|pears");
myString.Add("22.02|apples|pears");
myString.Add("99.02|apples|pears");
myString.Add("88.02|apples|pears");
myString.Add("77.02|apples|pears");
myString.Add("66.02|apples|pears");
var res = myString.Select(s => new
{
num = decimal.Parse(s.Split('|')[0]),
str = s
}).OrderByDescending(g => g.num).Take(5).Select(s => s.str);
res.Dump();
No error checking whatsoever, but it gives you the result:
100.02|apples|pears
99.02|apples|pears
88.02|apples|pears
77.02|apples|pears
66.02|apples|pears
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