Brand Wise使用LINQ销售3个项目

时间:2017-05-06 10:23:40

标签: c# sql linq

我需要在LINQ中编写一个查询,它将显示Brand Wise Top畅销3项。 我的结果就像这样

Result

如何在LINQ中编写此查询,任何人都可以帮忙。

此致 阿吉特

2 个答案:

答案 0 :(得分:-1)

您应该结合使用OrderByDesc和Take来获取集合中的三个最高项目。如果您想要查看每个品牌的结果,您需要在查询中添加GroupBy语句。

以下是一个例子:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        List<Brand> brands = new List<Brand>()
        {

            new Brand() {Name = "Brand 1", Item = "Item1", Qty = 200}, 
                new Brand() {Name = "Brand 1", Item = "Item1", Qty = 100}, 
                new Brand(){Name = "Brand 1", Item = "Item3", Qty = 98}, 
                new Brand() {Name = "Brand 1", Item = "Item4", Qty = 95}, 
                new Brand() {Name = "Brand 2", Item = "Item5", Qty = 150}, 
                new Brand() {Name = "Brand 2", Item = "Item6", Qty = 125}, 
                new Brand() {Name = "Brand 2", Item = "Item7", Qty = 120}};

        Console.WriteLine("Top items for all ");
        var topOfAll = brands.OrderByDescending(x => x.Qty).Take(3);        
        foreach(Brand brand in topOfAll)
        {
            Console.WriteLine(brand.Name + " " + brand.Item + " - " + brand.Qty);
        }

        IEnumerable<IGrouping<string, Brand>> grouped = brands.GroupBy(x => x.Name);
        foreach (IGrouping<string, Brand> groupedBrand in grouped)
        {
            Console.WriteLine("Top items for " + groupedBrand.Key);
            IEnumerable<Brand> top = groupedBrand.Select(x => x).OrderByDescending(x => x.Qty).Take(3);
            foreach (Brand i in top)
            {
                Console.WriteLine(i.Name + " " + i.Item + " - " + i.Qty);
            }
        }
    }
}

public class Brand
{
    public string Name
    {
        get;
        set;
    }

    public string Item
    {
        get;
        set;
    }

    public int Qty
    {
        get;
        set;
    }
}

此样本的结果是:

Top items for all Brand 1 Item1 - 200 Brand 2 Item5 - 150 Brand 2 Item6 - 125 Top items for Brand 1 Brand 1 Item1 - 200 Brand 1 Item1 - 100 Brand 1 Item3 - 98 Top items for Brand 2 Brand 2 Item5 - 150 Brand 2 Item6 - 125 Brand 2 Item7 - 120

此示例为此示例fiddle

答案 1 :(得分:-1)

通过下面的结果实现了结果。

       IQueryable<Sales> query = null;
       StringBuilder Query = new StringBuilder();

        Query.Clear();
        Query.Append("select *" + Environment.NewLine);
        Query.Append("from" + Environment.NewLine);
        Query.Append("(" + Environment.NewLine);
        Query.Append("    select brand, item, sum(qty) as qty, ROW_NUMBER() over(partition by brand order by sum(qty) desc) as row_num" + Environment.NewLine);
        Query.Append("    from sales" + Environment.NewLine);
        Query.Append("    group by brand, item" + Environment.NewLine);
        Query.Append(")t" + Environment.NewLine);
        Query.Append("where row_num <= 3" + Environment.NewLine);
        Query.Append("order by brand asc, qty desc" + Environment.NewLine);

        DataTable dt = GetDataTable(db, Query.ToString());

        query = (
                from rw in dt.AsEnumerable()
                select new Sales
                {
                    item = rw.Field<string>("item"),
                    brand = rw.Field<string>("brand"),
                    qty = rw.Field<int>("qty")
                }
              ).AsQueryable();


    public DataTable GetDataTable(DbContext context, string sqlQuery)
    {
        DbProviderFactory dbFactory = 
        DbProviderFactories.GetFactory(context.Database.Connection);

        using (var cmd = dbFactory.CreateCommand())
        {
            cmd.Connection = context.Database.Connection;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sqlQuery;
            using (DbDataAdapter adapter = dbFactory.CreateDataAdapter())
            {
                adapter.SelectCommand = cmd;

                DataTable dt = new DataTable();
                adapter.Fill(dt);

                return dt;
            }
        }
    }

希望此查询仅适用于MS Sql server。 再次感谢伙计..:D