开发者

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. 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜