4

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

3
  • 1
    How's that meant to work? When you group by columnZ, your select clause can not include non-aggregating function depending on columns other than columnZ itself. Can you explain us the broader problem you are trying to solve? Commented Nov 6, 2010 at 17:30
  • Group-by is an aggregating function; you would need to take something like a sum,min,max etc for that to make sense. Commented Nov 6, 2010 at 17:30
  • Okay, how about this: SELECT DISTINCT columnZ, (columnX + columnY) / 2 FROM DataTable I only want to send the second column to an array Commented Nov 6, 2010 at 17:34

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.