Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Linq Average

Tags:

c#

linq

I have a table with data similar to below:

Group    TimePoint    Value
  1          0          1
  1          0          2
  1          0          3
  1          1          3
  1          1          5

I want to project a table as such:

Group    TimePoint   AverageValue
  1          0            2
  1          1            4

EDIT: The data is in a datatable.

Anybody any ideas how this can be done with LINQ or otherwise?

Thanks.

like image 461
Darren Young Avatar asked Mar 15 '11 15:03

Darren Young


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


2 Answers

You need to perform Group By

The linq you need is something like:

var query = from item in inputTable
            group item by new { Group = item.Group, TimePoint = item.TimePoint } into grouped
            select new
            {
                Group = grouped.Key.Group,
                TimePoint = grouped.Key.TimePoint,
                AverageValue = grouped.Average(x => x.Value)
            } ;

For more Linq samples, I highly recommend the 101 Linq samples page - http://msdn.microsoft.com/en-us/vcsharp/aa336747#avgGrouped

like image 79
Stuart Avatar answered Sep 30 '22 00:09

Stuart


Here's a more function-oriented approach (the way I prefer it). The first line won't compile, so fill it in with your data instead.

var items = new[] { new { Group = 1, TimePoint = 0, Value = 1} ... };
var answer = items.GroupBy(x => new { TimePoint = x.TimePoint, Group = x.Group })
                  .Select(x => new { 
                                     Group = x.Key.Group,
                                     TimePoint = x.Key.TimePoint,
                                     AverageValue = x.Average(y => y.Value),
                                   }
                  );
like image 33
John Fisher Avatar answered Sep 30 '22 00:09

John Fisher