Linq 按产品排序但显示类别
Posted
技术标签:
【中文标题】Linq 按产品排序但显示类别【英文标题】:Linq sort by products but display cateogires 【发布时间】:2015-12-08 13:46:27 【问题描述】:所以我遇到了代码选择类别的问题 我需要更改排序顺序以按 product.Name 然后按 category.name 排序。
但问题是我还是想选择一个类别,但是我该怎么做 首先按 product.name 排序,无需添加额外的连接或选择。
从类别中的类别 选择类别名称 orderby category.Name //orderby类别名称
稍后在视图中我循环 foreach(category.products) 并传入 category.product[i] 以查看以显示
但是排序顺序是错误的,顺序总是按 Category.Name 如何先按 Product.Name 排序,然后按 Category.Name 排序? SelectMany 会有帮助吗?同样,我不想破坏选择 我的查询的一部分,只是 Order by stuff。
类产品 公共字符串名称 获取;放; 公共 int 类别 ID 获取;放;
类类别 公共字符串名称 获取;放; 公共 int ID 获取;放;
// 指定第一个数据源。 静态列表类别 = 新列表() 新类别()Name="Beverages", ID=001, new Category() Name="Condiments", ID=002, 新类别()名称=“蔬菜”,ID=003, 新类别()名称=“谷物”,ID=004, 新类别() Name="Fruit", ID=005 ;
// 指定第二个数据源。 静态列表产品 = 新列表() 新产品Name="Cola", CategoryID=001, 新产品Name="Tea", CategoryID=001, 新产品Name="Mustard", CategoryID=002, 新产品Name="Pickles", CategoryID=002, 新产品Name="Carrots", CategoryID=003, 新产品Name="Bok Choy", CategoryID=003, 新产品Name="Peaches", CategoryID=005, 新产品Name="Melons", CategoryID=005, ;
【问题讨论】:
对不起,我不能发表评论,但我犯了一个错误 class Category 列出哦,我看到你查询了,格式错误。
你需要order by Product.Name group by Category.Name
【讨论】:
【参考方案2】://LinqPad code
class Category
public string Name get; set;
public int ID get; set;
public List<Product> products get; set;
class Product
public string Name get; set;
public int ID get; set;
void Main()
// Specify the second data source.
List<Product> BevProducts = new List<Product>()
new ProductName="ZCola",
;
// Specify the third data source.
List<Product> CondProducts = new List<Product>()
new ProductName="Sugar",
;
// Specify the first data source.
List<Category> categories = new List<Category>()
new Category()Name="Beverages", ID=001, products=BevProducts,
new Category() Name="Condiments", ID=002, products=CondProducts,
;
var sortedCats = categories.OrderBy(c => c.ID).ToList();
foreach (var category in sortedCats)
//display category
System.Console.Out.WriteLine(category.Name);
//Assuming each category contains exactly one product in the list ie 1 to 1 relationship
// how can I sort by product.Name, so if ZCola comes before Sugar, ZCola's Category (Beverages) sorts before Condiments
// so in this Case Beverages, Condiments is the right order, because ZCola comes after Sugar.
var sortedProductsPerCategory = category.products.OrderBy(p => p.Name).ToList();
foreach (var product in sortedProductsPerCategory)
//display product
System.Console.Out.WriteLine(" " + product.Name);
【讨论】:
所以正确的顺序是 Sugar, ZCola 但这会打印 ZCola, Sugar【参考方案3】:class Category
public string Name get; set;
public int ID get; set;
public List<Product> products get; set;
class Product
public string Name get; set;
public int ID get; set;
void Main()
// Specify the second data source.
List<Product> BevProducts = new List<Product>()
new ProductName="ZCola",
;
// Specify the third data source.
List<Product> CondProducts = new List<Product>()
new ProductName="Sugar",
;
// Specify the first data source.
List<Category> categories = new List<Category>()
new Category()Name="Beverages", ID=001, products=BevProducts,
new Category() Name="Condiments", ID=002, products=CondProducts,
;
var sortedCats = categories.OrderBy(c => c.products.Min(p => p.Name)).ToList();
foreach (var category in sortedCats)
//display category
System.Console.Out.WriteLine(category.Name);
//Assuming each category contains exactly one product in the list ie 1 to 1 relationship
// how can I sort by product.Name, so if ZCola comes before Sugar, ZCola's Category (Beverages) sorts before Condiments
// so in this Case Beverages, Condiments is the right order, because ZCola comes after Sugar.
var sortedProductsPerCategory = category.products.OrderBy(p => p.Name).ToList();
foreach (var product in sortedProductsPerCategory)
//display product
System.Console.Out.WriteLine(" " + product.Name);
【讨论】:
以上是关于Linq 按产品排序但显示类别的主要内容,如果未能解决你的问题,请参考以下文章