In SQL, you can use
clauses to group data based on multiple columns. For example:
GROUP BY
SELECT * FROM <tablename> GROUP BY <column1>,<column2>
<> Use anonymous type
One method is to use anonymous type. The following is an example:
This code is grouped based on the
andvar groupedData = x.GroupBy(x => new { x.Column1, x.Column2 });
sequence to create an anonymous type for each unique combination of value. x
Column1
<> Data example Column2
Consider the following data structure:
and the following INSERT statement:
public class QuantityBreakdown { public int MaterialID { get; set; } public int ProductID { get; set; } public float Quantity { get; set; } }
INSERT INTO @QuantityBreakdown (MaterialID, ProductID, Quantity) SELECT MaterialID, ProductID, SUM(Quantity) FROM @Transactions GROUP BY MaterialID, ProductID
and
, and then calculates the sum of thevar groupedTransactions = transactions .GroupBy(t => new { t.MaterialID, t.ProductID }) .Select(g => new QuantityBreakdown { MaterialID = g.Key.MaterialID, ProductID = g.Key.ProductID, Quantity = g.Sum(t => t.Quantity) });
The above is the detailed content of How to Group Data by Multiple Columns in LINQ?. For more information, please follow other related articles on the PHP Chinese website!