You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varrows=extractedRows.AsQueryable();vardetailed=rows.GroupBy("new (Region, Product)").Select("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");// GROUPING SET 2: (Region)varregionSubtotal=rows.GroupBy("Region").Select("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");
How could I use Concat for detailed and regionSubtotal and order by Product the result using DynamicLINQ?
I need to calculate sorting expression using some metadata, so be able to have a string for sorting is a great help.
But I actually need to do in a code something like that:
CREATETABLESalesAlex (
Region VARCHAR(20),
Product VARCHAR(20),
Sales INT
);
INSERT INTO SalesAlex (Region, Product, Sales) VALUES
('North', 'Apples', 100),
('North', 'Oranges', 150),
('South', 'Apples', 200),
('South', 'Oranges', 250);
SELECT
Region,
Product,
SUM(Sales) AS TotalSales,
GROUPING_ID(Region, Product) AS GroupLevel
FROM SalesAlex
GROUP BY GROUPING SETS (
(Region, Product), -- Detailed totals
(Region), -- Subtotals by Region
() -- Grand total
)
I made that working with Dynamic LINQ, but also need to sort the result by Product for example.
varextractedRows=newList<SalesData>{new(){Region="North",Product="Widget",Sales="100"},new(){Region="North",Product="Gadget",Sales="150"},new(){Region="South",Product="Widget",Sales="200"},new(){Region="South",Product="Gadget",Sales="100"},new(){Region="North",Product="Widget",Sales="50"}};varrows=extractedRows.AsQueryable();// GROUPING SET 1: (Region, Product)vardetailed=rows.GroupBy("new (Region, Product)").Select<GroupedSalesData>("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");// GROUPING SET 2: (Region)varregionSubtotal=rows.GroupBy("Region").Select<GroupedSalesData>("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");varcombined=detailed.Concat(regionSubtotal);varordered=combined.OrderBy("Product").ToDynamicList();
This approach works really well - THANK YOU VERY MUCH -this is what I have now that is working with SAMPLE data table:
publicclassGroupedSalesData{publicstringRegion{get;set;}publicstring?Product{get;set;}publicintTotalSales{get;set;}publicintGroupLevel{get;set;}}publicclassSalesData{publicstringRegion{get;set;}=string.Empty;publicstringProduct{get;set;}=string.Empty;publicintSales{get;set;}}vareInfoJoinTable=newDataTable();eInfoJoinTable.Columns.Add("Region",typeof(string));eInfoJoinTable.Columns.Add("Product",typeof(string));eInfoJoinTable.Columns.Add("Sales",typeof(int));eInfoJoinTable.Rows.Add("North","Apples",100);eInfoJoinTable.Rows.Add("North","Oranges",150);eInfoJoinTable.Rows.Add("South","Apples",200);eInfoJoinTable.Rows.Add("South","Oranges",250);varextractedRows=eInfoJoinTable.AsEnumerable().Select(row =>newSalesData{Region=row.Field<string>("Region")??String.Empty,Product=row.Field<string>("Product")??String.Empty,Sales=row.Field<int?>("Sales")??0}).ToList();varrows=extractedRows.AsQueryable();// GROUPING SET 1: (Region, Product)vardetailed=rows.GroupBy("new (Region, Product)").Select<GroupedSalesData>("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");// GROUPING SET 2: (Region)varregionSubtotal=rows.GroupBy("Region").Select<GroupedSalesData>("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");// GROUPING SET 3: Grand Total (single row)vargrandTotal=rows.Select("new (Convert.ToInt32(Sales) as Sales)").GroupBy("true").Select<GroupedSalesData>("new (null as Region, null as Product, Sum(Sales) as TotalSales, 3 as GroupLevel)");varcombined=detailed.Concat(regionSubtotal).Concat(grandTotal);varordered=combined.OrderBy("Product desc").ToDynamicList();
The only issue is that I’ll be dynamically generating all the strings for grouping, ordering, etc. based on metadata. That’s why using Dynamic LINQ is a great approach. However, I don’t really know anything about the GroupedSalesData or SalesData classes — it seems to me it would be great if we could do same operations dynamically, without needing of this classes. :)
I see PR that eliminates need for both GroupedSalesData and SalesDataas I understand it. #913
varextractedRows=newList<SalesData>{new(){Region="North",Product="Widget",Sales="100"},new(){Region="North",Product="Gadget",Sales="150"},new(){Region="South",Product="Widget",Sales="200"},new(){Region="South",Product="Gadget",Sales="100"},new(){Region="North",Product="Widget",Sales="50"}};varrows=extractedRows.AsQueryable();// GROUPING SET 1: (Region, Product)vardetailed=rows.GroupBy("new (Region, Product)").Select("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");// GROUPING SET 2: (Region)varregionSubtotal=rows.GroupBy("Region").Select("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");varcombined=detailed.ToDynamicArray().Concat(regionSubtotal.ToDynamicArray()).AsQueryable();varordered=combined.OrderBy("Product").ToDynamicList();
Could you add another test please?
vareInfoJoinTable=newDataTable();eInfoJoinTable.Columns.Add("Region",typeof(string));eInfoJoinTable.Columns.Add("Product",typeof(string));eInfoJoinTable.Columns.Add("Sales",typeof(int));eInfoJoinTable.Rows.Add("North","Apples",100);eInfoJoinTable.Rows.Add("North","Oranges",150);eInfoJoinTable.Rows.Add("South","Apples",200);eInfoJoinTable.Rows.Add("South","Oranges",250);varextractedRows=fromrowineInfoJoinTable.AsEnumerable()selectrow;varrows=extractedRows.AsQueryable();// GROUPING SET 1: (Region, Product)vardetailed=rows.GroupBy("new (Region, Product)").Select("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");// GROUPING SET 2: (Region)varregionSubtotal=rows.GroupBy("Region").Select("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");varcombined=detailed.ToDynamicArray().Concat(regionSubtotal.ToDynamicArray()).AsQueryable();varordered=combined.OrderBy("Product").ToDynamicList();
Activity
StefH commentedon Apr 23, 2025
@agolovan
To get this working [with the current version], you need to create a class which defines the grouped result:
And use
.Select<GroupedSalesData>.Example:
StefH commentedon Apr 23, 2025
#907
agolovan commentedon Apr 23, 2025
This approach works really well - THANK YOU VERY MUCH -this is what I have now that is working with SAMPLE data table:
The only issue is that I’ll be dynamically generating all the strings for grouping, ordering, etc. based on metadata. That’s why using Dynamic LINQ is a great approach. However, I don’t really know anything about the GroupedSalesData or SalesData classes — it seems to me it would be great if we could do same operations dynamically, without needing of this classes. :)
I see PR that eliminates need for both GroupedSalesData and SalesDataas I understand it.
#913
Thanks a lot for your help!
agolovan commentedon Apr 24, 2025
I see you Q912 test.
Could you add another test please?
StefH commentedon Apr 24, 2025
@agolovan
I did add another test using DataTable, and with the new changes in the code, it seems to work fine.