Home
Insert
Update snippet 'Basic F# LINQ GroupBy example and multiple aggregates'
Title
Passcode
Description
Converting F# LINQ queries to SQL: - Basic example of simple group-by - Also if you want to have multiple aggregates (SUM, COUNT, MAX, MIN, AVG, ...) in the same SQL-clause, that is done with groupBy over a constant value.
Source code
// Multiple aggregates has to be done via groupBy constant even when SQL doesn't have it: // SELECT COUNT(1), SUM(UnitPrice) FROM Products let countAndSum = query { for p in context.Main.Products do groupBy 1 into g select (g.Count(), g.Sum(fun p -> p.UnitPrice)) } |> Seq.head // Basic Group by: // SELECT ShipCity, SUM(Freight) FROM Orders GROUP BY ShipCity let freightsByCity = query { for o in context.Main.Orders do groupBy o.ShipCity into cites select (cites.Key, cites.Sum(fun order -> order.Freight)) } |> Seq.toList
Tags
aggregate
average
count
groupby
linq
query
sql
sumby
aggregate
average
count
groupby
linq
query
sql
sumby
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update