Try .NET /elinq/
Powered by Try .NET

SQL Server HAVING

SQL Server HAVING with the COUNT function example

var query = DbContext.Set<CustomerYearOrders>()
                .Query((Orders order, CustomerYearOrders alias) =>
                {
                    var year = YEAR(order.OrderDate);
                    var customerId = order.CustomerId;
                    var count = COUNT(order.OrderId);

                    var result = SELECT<CustomerYearOrders>(customerId.@as(alias.Customer.CustomerId),
                        year.@as(alias.Year),
                        count.@as(alias.OrdersPlaced));
                    FROM(order);
                    GROUP(BY(customerId), BY(year));
                    HAVING(count >= 2);

                    return result;
                })
                .OrderBy(c => c.Customer)
                .Include(c => c.Customer);

foreach (var customer in query.Take(3))
    Console.WriteLine((customer.Customer.CustomerId, customer.Year));

SQL Server HAVING clause with the SUM() function example

var query = DbContext.Set<OrderNetSale>()
                .Query((OrderItems orderItems, OrderNetSale alias) =>
                {
                    var sum = SUM(orderItems.Quantity * orderItems.ListPrice * (1 - orderItems.Discount));

                    var result = SELECT<OrderNetSale>(orderItems.OrderId.@as(alias.Order.OrderId), sum.@as(alias.NetSale));
                    FROM(orderItems);
                    GROUP(BY(orderItems.OrderId));
                    HAVING(sum > 20000);

                    return result;
                })
                .OrderBy(c => c.NetSale)
                .Include(c => c.Order);

foreach (var orderNetValue in query.Take(3))
    Console.WriteLine((orderNetValue.Order.OrderId, orderNetValue.NetSale));

MAX(), MIN(), AVG() ...

All of them are regular functions in ELINQ. A query calling them can be construsted similarly.


< BACK | HOME