回答
您应该结合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。
尝试过什么,只是给出答案而没有看到OP的一方的努力,这是一个耻辱...... –
我不知道这是可能的在Linq单个查询。但我用一个单一的原始sql命令。 谢谢你的朋友:P –
@GiladGreen谢谢..:P –
实现了如下所示的结果。
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
- 1. Shopify隐藏畅销
- 2. 不是从畅销书
- 3. Magento畅销排行分类
- 4. 模拟amazon.com畅销书籍
- 5. 使用row-wise Reduate with mutate_?
- 6. 使用畅达
- 7. 如何使用Shopify在首页上显示畅销产品
- 8. 使用LINQ更新3
- 9. 需要不用Wise Installer覆盖文件
- 10. 的Rails获得最畅销的产品
- 11. 如何制作畅销品清单?
- 12. 如何在Prestashop中显示畅销书?
- 13. 查询选择最差产品畅销
- 14. Laravel查询 - 获得最畅销产品
- 15. Magento中的畅销书列表和catalog_product_flat_1
- 16. 寻找十大畅销产品
- 17. Opencart按畅销商品排序
- 18. Oscommerce修改多个畅销书
- 19. Jquery Mobile:禁用按钮样式Wise&Function Wise按钮直到发生事件后
- 20. Python 3中,并用的mysql畅达
- 21. Numpy row wise masking
- 22. sort duplicates date wise
- 23. 如何使用Hybridauth 3登录/注销?
- 24. 如何使用流畅的Apache组件
- 25. 流畅的NHibernate缓存Linq查询
- 26. Linq-to-SQL开销
- 27. python:if row [1] .upper()。find(brand)!= - 1:
- 28. T-SQL选择每个州最畅销的产品
- 29. Project wise创建配置文件
- 30. 如何查询我可以用来获得畅销书?
显示你的对象,你试过了什么? –
“我什么都没有试过,我都失去了想法! – Mardoxx
我们不是在这里为你写代码。 [问] – MickyD