[RunnableMethod]
public
void
MostExpensiveProductsPerCategory()
{
var
categories =
from
p in
northwind.Products
group
p by
p.CategoryID into
g
join
c in
northwind.Categories on
g.Key equals
c.CategoryID
select
new
{
CategoryID = g.Key,
CategoryName
= c.CategoryName,
MostExpensiveProducts =
from
p2 in
g
where
p2.UnitPrice == g.Max(p3 => p3.UnitPrice)
select
p2
};
foreach
(var
category in
categories)
{
Console.WriteLine("Category
ID: {0}, name: {1}",
category.CategoryID, category.CategoryName);
foreach
(var
product in
category.MostExpensiveProducts)
{
Console.WriteLine(" Product
ID: {0}, name: {1}, unit price: {2}",
product.ProductID, product.ProductName,
product.UnitPrice);
}
}
}
SELECT [t1].[CategoryID], [t2].[CategoryName],
[t1].[value]
FROM (
SELECT MAX([t0].[UnitPrice])
AS [value], [t0].[CategoryID]
FROM [dbo].[Products]
AS [t0]
GROUP BY [t0].[CategoryID]
) AS [t1]
INNER JOIN [dbo].[Categories]
AS [t2] ON [t1].[CategoryID] = ([t2].[CategoryID])
-- Context: SqlProvider(Sql2005)
Model: AttributedMetaModel Build:
3.5.21022.8
SELECT [t0].[ProductID],
[t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID],
[t0].[QuantityPerUnit], [t0].[UnitPrice],
[t0].[UnitsInStock], [t0].[UnitsOnOrder],
[t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
WHERE
([t0].[UnitPrice] = @x2) AND (((@x1 IS
NULL) AND ([t0].[CategoryID] IS NULL))
OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID]
IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
-- @x1: Input Int (Size = 0; Prec = 0;
Scale = 0) [1]
-- @x2: Input Money (Size
= 0; Prec = 19; Scale = 4) [263.5000]
-- Context: SqlProvider(Sql2005) Model:
AttributedMetaModel Build: 3.5.21022.8
Category ID: 1, name: Beverages
Product ID: 38, name: Côte de Blaye,
unit price: 263,5000
SELECT [t0].[ProductID],
[t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID],
[t0].[QuantityPerUnit], [t0].[UnitPrice],
[t0].[UnitsInStock], [t0].[UnitsOnOrder],
[t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
WHERE
([t0].[UnitPrice] = @x2) AND (((@x1 IS
NULL) AND ([t0].[CategoryID] IS NULL))
OR ((@x1 IS NOT NULL) AND ([t0].[CategoryID]
IS NOT NULL) AND (@x1 = [t0].[CategoryID])))
-- @x1: Input Int (Size = 0; Prec = 0;
Scale = 0) [2]
-- @x2: Input Money (Size
= 0; Prec = 19; Scale = 4) [43.9000]
-- Context: SqlProvider(Sql2005) Model:
AttributedMetaModel Build: 3.5.21022.8
Category ID: 2, name: Condiments
Product ID: 63, name: Vegie-spread, unit
price: 43,9000