locked
Lambda Average for ObserveableCollection RRS feed

  • Question

  • I have an observable collection with 15 columns (Call it OB_A).I pass OB_A into a method that creates a new temp observable collection (OB_B) that I need to populate. I need to go through each column 5, 6 and 7 of OB_A and get the current value plus the next 3 value and average these into OB_B. If there are 3 values after otherwise till the end of the collection.

    Is this possible with a Lambda expressions using the while and Average of collections?
    Or am I better off using foreach loops?

    eg

    OB_A                                                                        OB_B

    Col1 Col2 Col3 Col4 Col5 Col6 Col7                             Col1 Col2 Col3 Col4 Col5 Col6 Col7

    A11 B11  C11  D11 1.2   2.4   2.8                              A11 B11  C11  D11 1.5  2.4   2.8
    A22 B22  C22  D22 1.4   2.4   2.8                              A22 B22  C22  D22 1.7   2.4   2.8
    A33 B33  C33  D33 1.6   2.4   2.8                              A33 B33  C33  D33 1.9   2.4   2.8
    A44 B44  C44  D44 1.8   2.4   2.8                              A44 B44  C44  D44 2.1   2.4   2.8
    A55 B55  C55  D55 2.0   2.4   2.8                              A55 B55  C55  D55 2.3   2.4   2.8
    A66 B66  C66  D66 2.2   2.4   2.8                              A66 B66  C66  D66 2.4   2.4   2.8
    A77 B77  C77  D77 2.4   2.4   2.8                              A77 B77  C77  D77 2.5   2.4   2.8
    A88 B88  C88  D88 2.6   2.4   2.8                              A88 B88  C88  D88 2.6   2.4   2.8

    Tuesday, September 29, 2015 12:14 AM

Answers

  • Is the ordering in that list fixed/declared by another source? Or would the "next 5 nodes in both directions" change when you resort the whole thing?
    Would you want to order by that average?
    What does that average represent? Why is it exactly 5, nor 2 or 10? Could the range change?

    If the order is fixed/given, I would propably just take the type of OB_A and derive a new class with new fields/properties. Build OB_B (using the derived type). Fill OB_B from OB_A. Calcualte the averages. DisplayOB_B. Done.

    The other cases will get difficulty. Whole new scales of difficulty indeed.

    • Proposed as answer by Kristin Xie Wednesday, October 7, 2015 9:40 AM
    • Marked as answer by Kristin Xie Thursday, October 8, 2015 3:41 AM
    Tuesday, September 29, 2015 11:51 AM

All replies

  • Ok, I sort of have a part solution.

    I convert my OB_A (RawData) collection to a list of floats

    List<float> pulldownValues = new List<float>( RawData.Select(d=> d.Pulldown).ToList());

    I then create another list using my calculation

    var pulldownTemp = Enumerable.Range(0, pulldownValues.Count).Select(n => pulldownValues.Skip(n).Take(periodLength).Where(d => d > 0).Average()).ToList();

    which kind of gives me the results I want... But I now need the Average of the previous 5 results and the next 5 results (so a moving windowed average). if I use

    var pulldownTemp2 = Enumerable.Range(0, pulldownValues.Count).Select(n => pulldownValues.Skip((n - (int)(periodLength * 0.5))).Take(periodLength).Where(d => d > 0).Average()).ToList();

    It gives me the correct results from items 6 onwards but not for the first 5 rows.
    Is there a way to get a moving average using Lamdba on a List or Collection?

    Tuesday, September 29, 2015 3:55 AM
  • Is the ordering in that list fixed/declared by another source? Or would the "next 5 nodes in both directions" change when you resort the whole thing?
    Would you want to order by that average?
    What does that average represent? Why is it exactly 5, nor 2 or 10? Could the range change?

    If the order is fixed/given, I would propably just take the type of OB_A and derive a new class with new fields/properties. Build OB_B (using the derived type). Fill OB_B from OB_A. Calcualte the averages. DisplayOB_B. Done.

    The other cases will get difficulty. Whole new scales of difficulty indeed.

    • Proposed as answer by Kristin Xie Wednesday, October 7, 2015 9:40 AM
    • Marked as answer by Kristin Xie Thursday, October 8, 2015 3:41 AM
    Tuesday, September 29, 2015 11:51 AM