How to use C# Linq to find average value of a double list.

john zyd 421 Reputation points
2023-01-23T20:40:50.73+00:00

Hello:

I have the following data:

List<double[]> asks = new();

var ask1 = new[] { 4.0, 4.0 };

var ask2 = new[] { 5.0, 5.0 };

asks.Add(ask1);

asks.Add(ask2);

List<double[]> bids = new();

var bid1 = new[] { 3.0, 3.0 };

var bid2 = new[] { 2.0, 2.0 };

bids.Add(bid1);

bids.Add(bid2);

(List<double[]>, List<double[]>) price_data = new(asks, bids);

I want to use Linq to get the average of price.

This is how to calculate the average price:

The sum of the product of first item and the second item, divided by the sum of the second item for either asks or bids.

For example: the average price of asks = (4.0 * 4.0) + (5.0 * 5.0) / (4.0 + 5.0) = 41.0 / 9.0 = 4.55555…

the average price of bids = (3.0 * 3.0) + (2.0 * 2.0) / (3.0 + 2.0) = 2.6

So, I want to write a function, the input is the price_data, the output is (double, double) tuple with the first item being the average price of asks, and the second item for bids. The output is: (4.5, 2.6).

I know how to do this by some loops, but that seems ugly, I want to write some Linq code to make it look nice.

By the way, the double number always comes with endless number, like: 4.555555555555…

How can I limit the digit to maximum length to 10 when showing the number in screen?

Thanks,

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,190 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jack J Jun 24,276 Reputation points Microsoft Vendor
    2023-01-24T02:53:50.71+00:00

    @john zyd, Welcome to Microsoft Q&A, based on my test, you could use the following code to use linq to find average value of a double list.

     var result1 = from d1 in price_data.Item1
                              from d2 in price_data.Item2
                              select new
                              {
                                  id1 = d1.Aggregate((x, y) => (x * y)) / d1.Count(),
                                  id2 = d2.Aggregate((x, y) => (x * y)) / d1.Count(),
                                  id3 = d1.Sum() / (d1.Count()*2),
                                  id4 = d2.Sum() / (d2.Count()*2)
                              };
                var result2 = new Tuple<double, double>(result1.Sum(i => i.id1) / result1.Sum(i => i.id3), result1.Sum(i => i.id2) / result1.Sum(i => i.id4));
    
    

    Result:

    User's image

    Hope my code could help you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. john zyd 421 Reputation points
    2023-01-25T11:04:02.42+00:00

    Hi,

    I tried other data, the code seems not working.

    But I figure out another one, it works.

    But thanks for your help.

    Best Regards,