简体   繁体   English

C#DataTable LINQ&GROUP BY

[英]C# DataTable LINQ & GROUP BY

I have a DataTable with 20 columns (I only need 3 of them.) I need to perform the following query on it and then save the results as an array . 我有一个包含20列的DataTable (我只需要其中的3列。)我需要对它执行以下查询,然后将结果保存为array I've done some searching, but I can't figure out how to perform the mathematical operation. 我做了一些搜索,但我无法弄清楚如何进行数学运算。 I know LINQ should be used, but I'm not getting anywhere. 我知道应该使用LINQ,但我没有得到任何地方。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

SELECT DISTINCT columnZ, (columnX + columnY) / 2 FROM DataTable

*EDIT - corrected SQL statement *编辑 - 更正的SQL语句

Answering your last comment (I suggest you update the question): 回答你的上一条评论(我建议你更新问题):

  var result = 
    (from row in dataTable.AsEnumerable()
     let average = ((double)row["columnX"] + (double)row["columnY"])/2
     select new 
     {
        ColumnZ = (string)row["columnZ"],
        Average = average
     }).Distinct();

Use your actual data types. 使用您的实际数据类型。

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

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