用于自引用的嵌套 foreach table
Nested foreach for self referencing table
我正在努力为这段代码寻找不同的方法。
我想创建一个下拉列表 select 一个类别。
如您所见,它不干净并且不适用于多个级别。
我不是 .NET 方面的专家,但想了解专业人士是如何做到这一点的。
List<SelectListItem> list = new List<SelectListItem>();
foreach (Category item in db.CategorySet.Where(x => x.ParentCategory == null))
{
list.Add(new SelectListItem { Value = item.Id.ToString(), Text = item.Name });
foreach (Category subitem in item.SubCategories)
{
list.Add(new SelectListItem { Value = subitem.Id.ToString(), Text = " - " + subitem.Name });
foreach (Category subsubitem in subitem.SubCategories)
{
list.Add(new SelectListItem { Value = subsubitem.Id.ToString(), Text = " - - " + subsubitem.Name });
foreach (Category subsubsubitem in subsubitem.SubCategories)
{
list.Add(new SelectListItem { Value = subsubsubitem.Id.ToString(), Text = " - - - " + subsubsubitem.Name });
//...
}
}
}
}
public partial class Category
{
public Category()
{
this.Products = new HashSet<Product>();
this.SubCategories = new HashSet<Category>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public Nullable<int> ParentCategoryId { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<Category> SubCategories { get; set; }
public virtual Category ParentCategory { get; set; }
}
提前谢谢你...
您好像在制作层次结构树(使用 "-"
、"- -"
等)。
假设您的 Categories
是非循环的,您应该考虑使用递归函数来解决您的问题,传递您的 list
以及打印的前缀(“-”)或您的 "depth"关于递归搜索。
类似下面的内容可能会起作用:
public void addCatToList(List<SelectedItemList> list, int depth, IEnumerable<Category> cats){
foreach (Category item in cats)
{
list.Add(new SelectListItem { Value = item .Id.ToString(), Text = printDash(depth) + item.Name });
addCatToList(list, depth + 1, item.SubCategories);
}
}
private string printDash(int number){
string dash = string.Empty;
for(int i = 0; i < number; ++i){
if (i == 0)
dash += " ";
dash += "- ";
}
return dash;
}
然后你第一次调用它 depth = 0
:
List<SelectListItem> list = new List<SelectListItem>();
addCatToList(list, 0, db.CategorySet.Where(x => x.ParentCategory == null));
我正在努力为这段代码寻找不同的方法。 我想创建一个下拉列表 select 一个类别。 如您所见,它不干净并且不适用于多个级别。 我不是 .NET 方面的专家,但想了解专业人士是如何做到这一点的。
List<SelectListItem> list = new List<SelectListItem>();
foreach (Category item in db.CategorySet.Where(x => x.ParentCategory == null))
{
list.Add(new SelectListItem { Value = item.Id.ToString(), Text = item.Name });
foreach (Category subitem in item.SubCategories)
{
list.Add(new SelectListItem { Value = subitem.Id.ToString(), Text = " - " + subitem.Name });
foreach (Category subsubitem in subitem.SubCategories)
{
list.Add(new SelectListItem { Value = subsubitem.Id.ToString(), Text = " - - " + subsubitem.Name });
foreach (Category subsubsubitem in subsubitem.SubCategories)
{
list.Add(new SelectListItem { Value = subsubsubitem.Id.ToString(), Text = " - - - " + subsubsubitem.Name });
//...
}
}
}
}
public partial class Category
{
public Category()
{
this.Products = new HashSet<Product>();
this.SubCategories = new HashSet<Category>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public Nullable<int> ParentCategoryId { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<Category> SubCategories { get; set; }
public virtual Category ParentCategory { get; set; }
}
提前谢谢你...
您好像在制作层次结构树(使用 "-"
、"- -"
等)。
假设您的 Categories
是非循环的,您应该考虑使用递归函数来解决您的问题,传递您的 list
以及打印的前缀(“-”)或您的 "depth"关于递归搜索。
类似下面的内容可能会起作用:
public void addCatToList(List<SelectedItemList> list, int depth, IEnumerable<Category> cats){
foreach (Category item in cats)
{
list.Add(new SelectListItem { Value = item .Id.ToString(), Text = printDash(depth) + item.Name });
addCatToList(list, depth + 1, item.SubCategories);
}
}
private string printDash(int number){
string dash = string.Empty;
for(int i = 0; i < number; ++i){
if (i == 0)
dash += " ";
dash += "- ";
}
return dash;
}
然后你第一次调用它 depth = 0
:
List<SelectListItem> list = new List<SelectListItem>();
addCatToList(list, 0, db.CategorySet.Where(x => x.ParentCategory == null));