Try .NET /elinq/
Powered by Try .NET

SQL Server DENSE_RANK Function

Using SQL Server DENSE_RANK() function over a result set example

var query = DbContext.Set<ProductRank>()
                .Query((Products products, ProductRank alias) =>
                {
                    var priceRank = AggregateBy(DENSE_RANK())
                        .OVER(ORDER(BY(products.ListPrice).DESC));

                    var r = SELECT<ProductRank>(products.ProductId.@as(alias.Product.ProductId),
                        priceRank.@as(alias.Rank));
                    FROM(products);

                    return r;
                })
                .Include(r => r.Product);

foreach (var rank in query.Take(3))
    Console.WriteLine((rank.Product.ProductId, rank.Product.ProductName, rank.Product.ListPrice, rank.Rank));

Using SQL Server DENSE_RANK() over partitions example

var query = DbContext.Set<ProductRank>()
                .Query((Products products, ProductRank alias) =>
                {
                    var priceRank = AggregateBy(DENSE_RANK())
                        .OVER(PARTITION(BY(products.CategoryId))
                            .ORDER(BY(products.ListPrice).DESC));

                    var r = SELECT<ProductRank>(products.ProductId.@as(alias.Product.ProductId),
                        priceRank.@as(alias.Rank));
                    FROM(products);

                    return r;
                })
                .Where(r => r.Rank < 3)
                .Include(r => r.Product);

foreach (var rank in query.Take(3))
    Console.WriteLine((rank.Product.ProductId, rank.Product.ProductName, rank.Product.ListPrice, rank.Rank));

< BACK | HOME