Skip to content

Commit b038e62

Browse files
committed
Q912
1 parent 5759f26 commit b038e62

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

src-console/ConsoleApp_net6.0/Program.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,29 @@ public class Y
2121
{
2222
}
2323

24+
public class SalesData
25+
{
26+
public string Region { get; set; }
27+
public string Product { get; set; }
28+
public string Sales { get; set; }
29+
}
30+
31+
public class GroupedSalesData
32+
{
33+
public string Region { get; set; }
34+
public string? Product { get; set; }
35+
public int TotalSales { get; set; }
36+
37+
public int GroupLevel { get; set; }
38+
}
39+
2440
class Program
2541
{
2642
static void Main(string[] args)
2743
{
44+
Q912();
45+
return;
46+
2847
Json();
2948
NewtonsoftJson();
3049

@@ -39,7 +58,7 @@ static void Main(string[] args)
3958
{
4059
new X { Key = "x" },
4160
new X { Key = "a" },
42-
new X { Key = "a", Contestants = new List<Y> { new Y() } }
61+
new X { Key = "a", Contestants = new List<Y> { new() } }
4362
}.AsQueryable();
4463
var groupByKey = q.GroupBy("Key");
4564
var selectQry = groupByKey.Select("new (Key, Sum(np(Contestants.Count, 0)) As TotalCount)").ToDynamicList();
@@ -48,6 +67,36 @@ static void Main(string[] args)
4867
Dynamic();
4968
}
5069

70+
private static void Q912()
71+
{
72+
var extractedRows = new List<SalesData>
73+
{
74+
new() { Region = "North", Product = "Widget", Sales = "100" },
75+
new() { Region = "North", Product = "Gadget", Sales = "150" },
76+
new() { Region = "South", Product = "Widget", Sales = "200" },
77+
new() { Region = "South", Product = "Gadget", Sales = "100" },
78+
new() { Region = "North", Product = "Widget", Sales = "50" }
79+
};
80+
81+
var rows = extractedRows.AsQueryable();
82+
83+
// GROUPING SET 1: (Region, Product)
84+
var detailed = rows
85+
.GroupBy("new (Region, Product)")
86+
.Select<GroupedSalesData>("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");
87+
88+
// GROUPING SET 2: (Region)
89+
var regionSubtotal = rows
90+
.GroupBy("Region")
91+
.Select<GroupedSalesData>("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");
92+
93+
var combined = detailed.Concat(regionSubtotal);
94+
var ordered = combined.OrderBy("Product").ToDynamicList();
95+
96+
int x = 9;
97+
}
98+
99+
51100
private static void NewtonsoftJson()
52101
{
53102
var array = JArray.Parse(@"[

0 commit comments

Comments
 (0)