2017-05-06 14 views
-3

我需要在LINQ中写一个查询,它将显示Brand Wise最畅销3件商品。 我的结果会是这样Brand Wise畅销3件使用LINQ

Result

如何写LINQ任何人可以帮助请此查询。

问候, 阿吉特

+0

显示你的对象,你试过了什么? –

+0

“我什么都没有试过,我都失去了想法! – Mardoxx

+1

我们不是在这里为你写代码。 [问] – MickyD

回答

-1

您应该结合OrderByDesc并以获得集合中最高的三个项目。如果您想要查看每个品牌的结果,则需要为查询添加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

尝试过什么,只是给出答案而没有看到OP的一方的努力,这是一个耻辱...... –

+0

我不知道这是可能的在Linq单个查询。但我用一个单一的原始sql命令。 谢谢你的朋友:P –

+0

@GiladGreen谢谢..:P –

-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服务器。 再次感谢好友..:D