Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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
C# 有没有比计算更快的方法来查询数据表中的数据?_C#_Datatable - Fatal编程技术网

C# 有没有比计算更快的方法来查询数据表中的数据?

C# 有没有比计算更快的方法来查询数据表中的数据?,c#,datatable,C#,Datatable,我目前正在使用类似于此的函数计算几列数据 DataTable data = RetrieveDataTable(); var value = data.Compute( "AVG(Important_Variable)", "(Distance > 1230 and Distance < 1760) OR (Distance > 4710 and Distance < 5400)" ); DataTable data=RetrieveDataTable

我目前正在使用类似于此的函数计算几列数据

DataTable data = RetrieveDataTable();
var value = data.Compute(
    "AVG(Important_Variable)", 
    "(Distance > 1230 and Distance < 1760) OR (Distance > 4710 and Distance < 5400)"
);
DataTable data=RetrieveDataTable();
var值=data.Compute(
“平均值(重要变量)”,
(距离>1230且距离<1760)或(距离>4710且距离<5400)
);
这是工作,但它是缓慢的。有没有更快的方法实现同样的目标?不同的数据类型是完全可以的,我没有嫁给数据表

  • 将所有内容映射到POCO对象
  • 编写一个(只读)get属性……用于进行计算
  • 这是一个~basic~ ORM映射器(又名,创建您的POCO)

    附加使用对象其他属性的readonly(get;only)属性

    如果您的计算是“昂贵的”,并且您多次阅读它,那么您可以使用这种“可为空”的技巧


    我不知道您是如何填充数据表的,但我刚刚想到的一种方法是在填充数据表时累积元素的总和和计数(您需要计算平均值)。比如:

    int sum;
    int count;
    
    for(something) //Loop that populates the data table
    {
       .
       .
       var Important_Variable = Read something
       Populate(Important_Variable)
       count++;
       if((Distance > 1230 and Distance < 1760) || (Distance > 4710 and Distance < 5400))
          sum += Important_Variable;
       .
       .
    }
    var avg = sum/count;
    
    int和;
    整数计数;
    for(something)//填充数据表的循环
    {
    .
    .
    var Important_Variable=读取某些内容
    填充(重要的_变量)
    计数++;
    如果((距离>1230且距离<1760)| |(距离>4710且距离<5400))
    sum+=重要的_变量;
    .
    .
    }
    var avg=总和/计数;
    

    这样,您就不必在事后运行过滤器(我很确定这是非常耗时的部分)。

    为什么不在数据库上运行此查询?这是读取大量文本文件(想想100到1000)的例程的一部分,读取文本文件中的数字,将它们解析为每个文件的DataTable,该文件通常有大约1.5k行,然后从这些表中计算度量值。如果我执行POCO对象并创建一个列表并对它们运行LINQ查询,这会比DataTable快吗?@PlTaylor根据您编写LINQ查询的方式,它可能能够执行类似
    aspallel()的操作
    ,并使用并行功能来提高性能。您需要对其进行基准测试。读取的每个文件都已以并行方式完成,因此AsParallel()可能没有帮助。从他正在执行的操作(平均值)来看,我认为他无法将其并行化。您确定
    Compute吗(
    是并行进行的吗?@SystemDown我将我的旧注释更新为内置的并行平均值函数。只需将其与
    Where(
    子句连接,以过滤范围。这是一个有趣的想法。我必须将其原型化。
    int sum;
    int count;
    
    for(something) //Loop that populates the data table
    {
       .
       .
       var Important_Variable = Read something
       Populate(Important_Variable)
       count++;
       if((Distance > 1230 and Distance < 1760) || (Distance > 4710 and Distance < 5400))
          sum += Important_Variable;
       .
       .
    }
    var avg = sum/count;