开发者

C# DataTable LINQ & GROUP BY

开发者 https://www.devze.com 2023-01-24 00:17 出处:网络
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. I\'ve done some searching, but I can\'t figure out how to

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. 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. Any help i开发者_开发技巧s greatly appreciated!

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

*EDIT - corrected SQL statement


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消