asp.net MVC 3的WebGrid必将列出<动态>是极其缓慢 [英] asp.net mvc 3 webgrid bound to List<dynamic> is exceedingly slow

查看:129
本文介绍了asp.net MVC 3的WebGrid必将列出<动态>是极其缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示的数据表,可以在视图中返回一个可变的列数,所以我绑定的MVC 3的WebGrid到List<动态&作为答案所描述的这个帖子GT ;,:的Populate MVC的WebGrid从数据表

它工作正常,但令人难以置信的是慢!所谓慢得令人难以置信我的意思是需要13秒11列,以显示一组的15条记录。有没有什么办法可以加快这?我试着删除寻呼机但没有效果。

在code,创建列表<动态>从Ado.Net数据表看起来像这样。它在这里的运行速度很快,没问题。

  VAR MYLIST =新的List<动态>();
的foreach(在MyTable.Rows的DataRow行)
{
   无功的expando =(IDictionary的<字符串对象>)新ExpandoObject();
   的foreach(在COLUMNNAMES字符串列)
   {
       expando.Add(列,行[专栏]);
   }
   MyList.Add(Expando的);
}

在视图中出现问题。下面的code大约需要13秒呈现一组15条记录与11列!此行的数据库和数据表到List&LT转换;动态>花费不到一秒。下面的code需要13秒,刚呈现。我究竟做错了什么?还是我只是隔靴搔痒使用列表&LT错了;动态>

  VAR电网=新的WebGrid(canPage:真,rowsPerPage:Model.PageSize,canSort:假);
 grid.Bind(Model.MyList,rowCount等:(INT)Model.RowCount,autoSortAndPage:假);
 grid.Pager(WebGridPagerModes.All);
                @ grid.GetHtml(TABLESTYLE的WebGrid
                    rowStyle的WebGrid行
                    alternatingRowStyle的WebGrid-交替行
                         htmlAttributes:新{ID =tblSearchResults},
                         firstText:<<第一
                         lastText:去年>>中,模式:WebGridPagerModes.All
                )


解决方案

我有同样的问题,这是很烦人的,因为我们加载了任意数据。

解决方法是定义列和这些列的格式功能,这由一个巨大的因素改善的WebGrid性能和扩展性能远好(基于列数)比没有定义的格式。

例如

 的IEnumerable< KeyValuePair<字符串对象>>例如= data.First();
的foreach(例如VAR对)
    cols.Add(grid.Column(
        COLUMNNAME:pair.Key,
        //似乎指定格式保存网页电网可笑大量的时间
        格式:(项目=>((IDictionary的<弦乐,对象>)item.Value)pair.Key])
    ));

请参阅我的小例子项目 https://github.com/jamesk/MVCWebGridSpeed​​Test 的速度对比而完整的例子code。

I need to display a data table that can return a variable number of columns in a view, so I am binding an Mvc 3 WebGrid to a List<dynamic>, as described in the answer to this post: Populate MVC Webgrid from DataTable

It works fine, but is incredibly slow! By "incredibly slow" I mean it takes 13 seconds to display a set of 15 records with 11 columns. Is there any way to speed this up? I've tried removing the Pager but that has no effect.

The code that creates the List<dynamic> from an Ado.Net data table looks like this. It runs very quickly, no problem here.

var MyList = new List<dynamic>();
foreach (DataRow row in MyTable.Rows)
{
   var expando = (IDictionary<string, object>)new ExpandoObject();
   foreach (string column in columnNames)
   {
       expando.Add(column, row[column]);
   }
   MyList.Add(expando);
}

The problem occurs in the view. The code below takes about 13 seconds to render a set of 15 records with 11 columns! The trip to the database and the conversion of the data table to a List<dynamic> takes less than a second. The code below takes 13 seconds, just to render. What am I doing wrong? Or am I just barking up the wrong tree using a List<dynamic>?

var grid = new WebGrid(canPage: true, rowsPerPage: Model.PageSize, canSort: false);
 grid.Bind(Model.MyList, rowCount: (int)Model.RowCount, autoSortAndPage: false);
 grid.Pager(WebGridPagerModes.All);
                @grid.GetHtml(tableStyle: "webgrid",
                    rowStyle: "webgrid-row",
                    alternatingRowStyle: "webgrid-alternating-row",
                         htmlAttributes: new { id = "tblSearchResults" },
                         firstText: "<<First",
                         lastText: "Last>>", mode: WebGridPagerModes.All
                )

解决方案

I had the same issue and it was very annoying because we were loading up arbitrary data.

The fix is to define columns and a format function for those columns, this improves the WebGrid performance by a HUGE factor and the performance scales far better (based on number of columns) than without format defined.

e.g.

IEnumerable<KeyValuePair<string, Object>> example = data.First();
foreach (var pair in example)
    cols.Add(grid.Column(
        columnName: pair.Key,
        //Seems that specifying a formatter saves the web grid ridiculous amounts of time
        format: (item => ((IDictionary<String, Object>)item.Value)[pair.Key])
    ));

See my little example project https://github.com/jamesk/MVCWebGridSpeedTest for a speed comparison and full example code.

这篇关于asp.net MVC 3的WebGrid必将列出&LT;动态&GT;是极其缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆