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 is greatly appreciated!

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

*EDIT – corrected SQL statement

LinQ & DataTable Group By Question in C#

I have a DataTable and stores 4 columns (StockCardCode,Explain,Quantity,BranchCode), and Im grouping them with a LinQ. Code is my key for using Group By. When I write my query, I just can use 2 column



Use LINQ to group data from DataTable

I want to use LINQ to group data from a DataTable (columns: userid, chargetag, charge). The content could look like this: userid chargetag charge —————————– user1 tag3 100 user2 tag

Linq C# Groupby on datatable

I would like to write a Linq query to apply group-by on DataTable, which outputs the result as a DataTable. I tried this code, which returns a result as var type, but I want the result in DataTable va

c# LINQ DataTable substraction

i have two DataTables which has different columns but one column is same. i want to get new DataTable which contains row from dt1 which are not exist in dt2. how can i do it using LINQ? that’s is my c

Group using linq return datatable

i have a linq query that returns Articles ordered by the number of tags that match the current article e.g current article has tags – tag1, tag2, tag3 tagged article 1 – tag1,tag2,tag3 tagged article

VB.NET & Linq: Sum datatable column and Group by date in another column

I have a typedDataset (ds) and its datatable(dt). The datatable has the columns Date(ttmmyyy) and Worktime. I want to sum the Worktime by month. I already accomplished to sum the Worktime with Dim sum

Linq Group & SUM

I really thought I was getting on with linq until now, Ive got a one to many relationship and I need to return the SUM for the Jobitems (jobitems.Cost) List<Models.Inventory.JobItem> jobItems =

How to group by on multiple columns from datatable with linq? [duplicate]

This question already has an answer here: Group By Multiple Columns 4 answers I am using Linq to group by some columns in my datatable List<DataTable> tables = ds .Tables[0] .AsEnumerabl

c# Linq from DataTable

Hi there I have a small DataTable that contains a number of rows which i am doing a LINQ query against. I am having problems getting the LINQ to display the text that is in the datatable. When I run

c # using linq to group by multiple columns in a datatable

I have three columns in a datatable: string, DateTime, and decimal. I want to group by the string and decimal column, and for the rows grouped I want to sum the decimal values. I know how to do the su

Answers

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.