Mr. Roach
Mr. Roach
CC#
Created by Mr. Roach on 7/3/2024 in #help
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 orderby 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
;
12 replies
CC#
Created by Mr. Roach on 6/28/2024 in #help
Microsoft DataFrame example not working
Hello there, I'm trying to understand Microsoft DataFrames for a c# project where I need to sum data of prices of items that have the same names inside a csv(sum costs for each entry of "apple", "banana" etc). In my python version I used pandas for that and pivoted after dropping not needed columns to achieve what I wanted with few lines of code. But now I'm stuck already trying to follow the examples provided by Microsoft for DataFrames. I tried to copy the code mentioned in the "Combine Data Sources" but I'm getting error that column "id" wouldn't exist. Does someone know how good the Microsoft Website is for getting into DataFrames or is there a better place or solution to achive what I want?
var ids = new List<Single>() { 1, 2, 3, 4, 5, 6 };
var bedrooms = new List<Single>() { 1, 2, 3, 2, 3, 1 };

var idColumn = new SingleDataFrameColumn("Id", ids);
var bedroomColumn = new SingleDataFrameColumn("BedroomNumber", bedrooms);
var dataFrame2 = new DataFrame(idColumn, bedroomColumn);

dataFrame = dataFrame.Merge(dataFrame2, new string[] { "Id" }, new string[] { "Id" });

DataFrame.SaveCsv(dataFrame, "path\\result.csv", ',');
var ids = new List<Single>() { 1, 2, 3, 4, 5, 6 };
var bedrooms = new List<Single>() { 1, 2, 3, 2, 3, 1 };

var idColumn = new SingleDataFrameColumn("Id", ids);
var bedroomColumn = new SingleDataFrameColumn("BedroomNumber", bedrooms);
var dataFrame2 = new DataFrame(idColumn, bedroomColumn);

dataFrame = dataFrame.Merge(dataFrame2, new string[] { "Id" }, new string[] { "Id" });

DataFrame.SaveCsv(dataFrame, "path\\result.csv", ',');
https://learn.microsoft.com/en-us/dotnet/machine-learning/how-to-guides/getting-started-dataframe
24 replies