Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 循环优化(多级菜单)ASP.NET_Asp.net Mvc_Entity Framework_File - Fatal编程技术网

Asp.net mvc 循环优化(多级菜单)ASP.NET

Asp.net mvc 循环优化(多级菜单)ASP.NET,asp.net-mvc,entity-framework,file,Asp.net Mvc,Entity Framework,File,我正在尝试创建一个函数来为多语言菜单生成HTML文件。代码可以工作,但唯一的问题是编译时间太长。流程是,在启动程序之前,将在Global.asax文件进程中调用此函数 我怀疑这是由循环过程引起的。所以请给我一些关于优化的建议。先谢谢你。这是代码 public static void GenerateMenu(string strPath) { string[] tempCateHTML = new string[] { "_temp_menu_en.cshtml", "_temp_me

我正在尝试创建一个函数来为多语言菜单生成HTML文件。代码可以工作,但唯一的问题是编译时间太长。流程是,在启动程序之前,将在Global.asax文件进程中调用此函数

我怀疑这是由循环过程引起的。所以请给我一些关于优化的建议。先谢谢你。这是代码

 public static void GenerateMenu(string strPath)
{
    string[] tempCateHTML = new string[] { "_temp_menu_en.cshtml", "_temp_menu_bm.cshtml", "_temp_menu_ch.cshtml" }; //temporary file names
    string[] cateHTML = new string[] { "_menu_en.cshtml", "_menu_bm.cshtml", "_menu_ch.cshtml" };
    for (int i = 0; i < 3; i++)
    {

        using (EFContext ctx = new EFContext())
        {
            List<int> parentId = new List<int>();
            List<Category> parentCategory = ctx.Database.SqlQuery<Category>
                ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = 0").ToList();

            StringWriter sWriter = new StringWriter();
            using (HtmlTextWriter wt = new HtmlTextWriter(sWriter))
            {
                 foreach (Category cate in parentCategory)
                {

                    string[] columnParent = new string[] { cate.Category_Name, cate.Category_NameBM, cate.Category_NameCH };
                    wt.RenderBeginTag(HtmlTextWriterTag.Li); //<li>
                    wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                    wt.RenderBeginTag(HtmlTextWriterTag.A); //<a>
                    wt.Write(columnParent[i]);
                    wt.RenderEndTag();//</a>

                    wt.RenderBeginTag(HtmlTextWriterTag.Ul);//<ul>

                    List<Category> childCategoryL1 = ctx.Database.SqlQuery<Category>
                        ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = {0}", cate.CategoryID).ToList();

                   foreach (Category root in childCategoryL1)
                    {
                        string[] columnChild1 = new string[] { root.Category_Name, root.Category_NameBM, root.Category_NameCH };
                        wt.RenderBeginTag(HtmlTextWriterTag.Li);//<li>
                        wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                        wt.RenderBeginTag(HtmlTextWriterTag.A);//<a>
                        wt.Write(columnChild1[i]);
                        wt.RenderEndTag();//</a>

                        List<Category> childCategoryL2 = ctx.Database.SqlQuery<Category>
                            ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = {0}", root.CategoryID).ToList();

                        if (childCategoryL2.Count > 0)
                        {
                            wt.RenderBeginTag(HtmlTextWriterTag.Ul);//<ul>

                            foreach (Category child1 in childCategoryL2)
                            {
                                string[] columnChild2 = new string[] { child1.Category_Name, child1.Category_NameBM, child1.Category_NameCH };
                                wt.RenderBeginTag(HtmlTextWriterTag.Li);//<li>
                                wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                                wt.RenderBeginTag(HtmlTextWriterTag.A);//<a>
                                wt.Write(columnChild2[i]);
                                wt.RenderEndTag();//</a>

                                List<Category> childCategoryL3 = ctx.Database.SqlQuery<Category>
                                    ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = {0}", child1.CategoryID).ToList();

                                if (childCategoryL3.Count > 0)
                                {
                                    wt.RenderBeginTag(HtmlTextWriterTag.Ul);//<ul>

                                    foreach (Category child2 in childCategoryL3)
                                    {
                                        string[] columnChild3 = new string[] { child2.Category_Name, child2.Category_NameBM, child2.Category_NameCH };
                                        wt.RenderBeginTag(HtmlTextWriterTag.Li);//<li>
                                        wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                                        wt.RenderBeginTag(HtmlTextWriterTag.A);//<a>
                                        wt.Write(columnChild3[i]);
                                        wt.RenderEndTag();//</a>
                                        wt.RenderEndTag();//</li>
                                    }
                                    wt.RenderEndTag();//</ul>
                                }
                                wt.RenderEndTag();//</li>
                            }

                            wt.RenderEndTag();//</ul>
                        }
                        wt.RenderEndTag();//</li>
                    }

                    wt.RenderEndTag();//</ul>
                    wt.RenderEndTag();//</li>
                }
            }


            string menuHTML = sWriter.ToString();

            string filePath = Path.Combine(strPath, @"Views\Shared\CacheData\CateMenu\");
            new FileInfo(filePath).Directory.Create(); //create new folder
            var menuPath = String.Format("{0}{1}", filePath, tempCateHTML[i]); //create multiple HTML files for multiple language

            using (FileStream fs = new FileStream(menuPath, FileMode.Append, FileAccess.Write))
            {
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(menuHTML);
                sw.Flush();
                sw.Close();
                fs.Close();
            }

            if (!File.Exists(filePath + cateHTML[i]))
            {
                using (File.Create(filePath + cateHTML[i]))
                {
                    //create dummy file if the file doesnt exists
                }
            }

            File.Replace(menuPath, filePath + cateHTML[i], filePath + cateHTML[i] + ".bac");
        }
    }

}

这是一个漫长的过程。因此,非常感谢您抽出时间

实际上我已经找到了解决问题的方法。该解决方案是基于树菜单构建的。谢谢您的帮助。

这似乎太多查询,无法构建菜单。显然,我不知道您正在做什么的细节,但我建议放弃这种方法,考虑在存储过程中使用递归查询生成菜单结构来完成所有工作,并向您提供注释,实际上,我不介意放弃这种方法,但我想不出任何更好的方法来编写HTML,从数据库获取数据,生成多级菜单。请提供任何这样做的例子。