Questions regarding a linq query
Hello there, I'm playing a little bit with linq queries but don't know if i truly understand this. Why do I need to
orderbyorderby twice here(and group again) in order to get the full result and the parts its made of ordered how I want? I read something about primary and secondary sort and wondered if thats whats happening here. I'm also wondering if this query could be written better/smaller? from sales in File.ReadAllLines(file).Skip(1)
select sales.Split(",") into sales
select (itemName: sales[1], stackSize: sales[2], quantity: decimal.Parse(sales[3]), price: decimal.Parse(sales[4]), otherPlayer: sales[5]) into grouped
// grouping the selection by otherPlayer and itemNames
group grouped by new { grouped.otherPlayer, grouped.itemName } into grouped
// therefore selecting for each player a grouped view where the values
// for quantity and price of each item that appears gets summed
select new
{
grouped.Key.otherPlayer,
item = grouped.Key.itemName,
quant = grouped.Sum(x => x.quantity),
price = grouped.Sum(x => x.price) / 10000
}
into grouped
// here the results for each otherPlayer but not the whole query are getting ordered(secondary sort?)
orderby grouped.price descending
// if i dont group here again, I cant order by the summed values that each player has, why?
group grouped by new { grouped.otherPlayer } into grouped
// this order makes the whole query ordered(primary sort?)
orderby grouped.Sum(x => x.price) descending
select grouped
; from sales in File.ReadAllLines(file).Skip(1)
select sales.Split(",") into sales
select (itemName: sales[1], stackSize: sales[2], quantity: decimal.Parse(sales[3]), price: decimal.Parse(sales[4]), otherPlayer: sales[5]) into grouped
// grouping the selection by otherPlayer and itemNames
group grouped by new { grouped.otherPlayer, grouped.itemName } into grouped
// therefore selecting for each player a grouped view where the values
// for quantity and price of each item that appears gets summed
select new
{
grouped.Key.otherPlayer,
item = grouped.Key.itemName,
quant = grouped.Sum(x => x.quantity),
price = grouped.Sum(x => x.price) / 10000
}
into grouped
// here the results for each otherPlayer but not the whole query are getting ordered(secondary sort?)
orderby grouped.price descending
// if i dont group here again, I cant order by the summed values that each player has, why?
group grouped by new { grouped.otherPlayer } into grouped
// this order makes the whole query ordered(primary sort?)
orderby grouped.Sum(x => x.price) descending
select grouped
;