简体   繁体   English

C#解析数据行

[英]C# Parsing Datarows

Scenario: 场景:

Based on user interaction, one of several stored procedures will be called, each having different result structure, so I can't strongly type the return. 基于用户交互,将调用多个存储过程之一,每个存储过程具有不同的结果结构,因此我不能强力键入返回值。

I am using Petapoco and returning IEnumerable<dynamic> rows, converting to a datatable (which takes about 245ms) then looping through the rows in the datatable's rows and appending to a string creating an html table. 我正在使用Petapoco并返回IEnumerable<dynamic>行,转换为数据表(大约需要245ms),然后循环遍历数据表行中的行,并附加到创建html表的字符串中。

One result is returning over 3000 lines and is taking almost 2 minutes to parse. 结果是返回了3000多个行,并且花费了将近2分钟的时间来解析。

How can I improve this? 我该如何改善? Each row is only taking around 35ms so individually not a problem. 每行仅需要35毫秒左右的时间,因此单独处理不会出现问题。

I'd rather not have to add pagination if I can get away with it. 如果我可以摆脱它,我宁愿不必添加分页。

Most likely using a StringBuilder is the solution. 解决方案很可能使用StringBuilder

Instead of appending to a string instance: 而不是附加到字符串实例:

var html = "";

foreach(var row in rows)
    html += "<p>" + row + "</p>";

Use a StringBuilder : 使用一个StringBuilder

var sb = new StringBuilder();
foreach(var row in rows)
    sb.Append("<p>" + row + "</p>");

var html = sb.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM