0

(I don't know if I should call them objects or elements, sorry if I typed the wrong name).

I have this array:

data[] readings; 

If I want the value I use the following( i = some position):

readings[i].value;

And if I want the date:

readings[i].date;

How can I get the average of all value in readings using the average method?

1

3 Answers 3

2

you can use linq for this, something along the lines of

var average = readings.Average(r => r.value);

Note: Average Computes the average of a sequence of numeric values. MSDN Link: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.average.aspx

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

2 Comments

Yes I tried this a few minutes ago but it didnt' work, it needs a cast because it says it can't convert object to float. So I did this and now it works: readings.Average(r => Convert.ToDouble(r.value));
@sparcopt Ah, cool, wasn't sure what the type was, but I am glad it worked out! I'll update the answer
1

If data.value is a numeric type you can use Linq's Average extension method, like this:

var averageValue = readings.Average(d => d.value);

To get an average of a DateTime, you can do the same thing if you the date to clock ticks first:

var averageDate = new DateTime((long)readings.Average(d => d.date.Ticks));

Comments

0

Try a Linq query:

readings.Select(r => r.value).ToList().Average();

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.