所以我遇到的问题是代码选择类别 我需要更改排序顺序以按产品排序。名称然后按category.name。
但问题是我还是想选择一个类别,但我该怎么做 首先按product.name排序,不要添加额外的连接或选择。
来自类别的类别 选择category.name orderby category.Name // orderby category name
稍后在视图中我循环foreach(category.products)并传入category.product [i]以查看显示
但排序顺序错误,顺序始终是Category.Name 我如何先按Product.Name排序,再按Category.Name排序? SelectMany会帮忙吗?我再次不想破坏选择 我的查询的一部分,只是按订单排序。
类产品 { public string Name {get;组; } public int CategoryID {get;组; } }
类别 { public string Name {get;组; } public int ID {get;组; } }
//指定第一个数据源。
静态列表类别=新列表()
{
new Category(){Name =" Beverages",ID = 001},
新类别(){名称="调味品",ID = 002},
new Category(){Name =" Vegetables",ID = 003},
new Category(){Name =" Grains",ID = 004},
new Category(){Name =" Fruit",ID = 005}
};
//指定第二个数据源。 static List products = new List() { 新产品{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}, };
答案 0 :(得分:0)
哦,我看到你查询了,格式很糟糕。
您需要order by Product.Name group by Category.Name
答案 1 :(得分:0)
//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 Product{Name="ZCola"},
};
// Specify the third data source.
List<Product> CondProducts = new List<Product>()
{
new Product{Name="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);
}
}
}
答案 2 :(得分:0)
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 Product{Name="ZCola"},
};
// Specify the third data source.
List<Product> CondProducts = new List<Product>()
{
new Product{Name="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);
}
}
}