嵌套的foreach用于自引用表

时间:2017-05-05 01:41:19

标签: c# asp.net-mvc public

我正在努力为此代码找到不同的方法。 我想创建一个下拉列表来选择一个类别。 正如您所看到的那样,它不干净,不适用于多个级别。 我不是.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; }
}

提前谢谢你......

1 个答案:

答案 0 :(得分:1)

好像你正在制作层次结构树(使用public void animateMarker(final GoogleMap mMap, final Marker marker, final List<LatLng> arrayLications, final double duration, final int step) { Log.d(TAG, "Moving from:" + step + "To:" + (step + 1) + "duration" + duration); final Handler handler = new Handler(); final long start = SystemClock.uptimeMillis(); LatLng startPosition = arrayLications.get(step); final LatLng toPosition = arrayLications.get(step + 1); Projection proj = mMap.getProjection(); Point startPoint = proj.toScreenLocation(startPosition); final LatLng startLatLng = proj.fromScreenLocation(startPoint); final Interpolator interpolator = new LinearInterpolator(); handler.post(new Runnable() { @Override public void run() { long elapsed = SystemClock.uptimeMillis() - start; float t = interpolator.getInterpolation((float) (elapsed / duration)); Log.d(TAG, "interpolator t:" + t + "start :" + start + " elapsed :" + elapsed); double lng = t * toPosition.longitude + (1 - t) * startLatLng.longitude; double lat = t * toPosition.latitude + (1 - t) * startLatLng.latitude; LatLng newLatLng = new LatLng(lat, lng); Log.d(TAG, "Moving to" + newLatLng); marker.setPosition(newLatLng); if (t < 1.0) { handler.postDelayed(this, 100); } else { if (arrayLications.size() > step + 2 && isVisible()) { double distance = distance(arrayLications.get(step + 1), arrayLications.get(step + 2)); double time = distance * msForKMTravel; Log.d(TAG, "Go for distance" + distance + " in time " + time); animateMarker(mMap, marker, arrayLications, time, step + 1); } } } }); } "-"等等。)

假设您的"- -"是非循环的,您应该考虑使用递归函数来解决您的问题,传递Categories以及您打印的前缀(“ - ”)或您的“深度”关于递归搜索。

下面的内容可能有用:

list

然后您第一次使用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