-1

Working code.

  double[] confirmingList = new double[] { -3, -2, -1 };
    
    var Tdb = rDatas
        .Select(d => new
        {
            TScore = (new List<double> {
                d.T15Min, // Let this count .2
                d.T30Min, // .25
                d.T65Min, // .3
                d.T130Min, // .4
                d.T195Min, // .5
                d.TDaily,  // .8
                d.TWeekly // 1
                }).Count(c => confirmingList.Contains(c))

What I want is to aggregate by time frame match and weighting by property. Is there some Linq Kung Fu that does that? Should I have some class that I pass to multiply weighting by property name? I suspect there is something in the aggregate operator that would allow for this?

Can't really find something that shows a logical operation match while aggregating values with a modification or weighting.

SuperDave
  • 373
  • 5
  • 14
  • 2
    I didn't get what is exactly your need, can you improve you explanation/code? – Andre.Santarosa Jan 03 '21 at 19:52
  • I have properties and if those properties match one from the array, then I want to add a score to the total. The added value will be scaled higher for each time frame and added to the score. This is to weight the longer time frames more than the shorter time frames. – SuperDave Jan 03 '21 at 20:58
  • @SuperDave Please try to share with us as much code as needed to be able to reproduce the problem on anyone's machine. – Peter Csala Jan 04 '21 at 07:45
  • Since I don't know the answer I can't really put out code. As stated I don't just want to count matched items in a list but match them and weight the results on which time frame they are in. the code above matches and counts the matches. How would one then add a formula for which property they matched and aggregate the total. – SuperDave Jan 04 '21 at 13:32
  • What is "a formula for which property they matched"? Perhaps if you added some actual data and results? Where are the weights? – NetMage Jan 04 '21 at 19:23
  • As in the first example a match of - 3, -2, or 1 in the T15Min property would be .2 toward the total for all properties. T30 would be .25 and T65 would be .3 etc. – SuperDave Jan 05 '21 at 02:28

1 Answers1

1

Using the indexed Select method, you can match each timeframe to its weight when it should be counted:

double[] confirmingList = new double[] { -3, -2, -1 };
var weights = new[] { 0.2, 0.25, 0.3, 0.4, 0.5, 0.8, 1.0 };

var Tdb = rDatas
    .Select(d => new {
        TScore = (new[] {
            d.T15Min, // Let this count .2
            d.T30Min, // .25
            d.T65Min, // .3
            d.T130Min, // .4
            d.T195Min, // .5
            d.TDaily,  // .8
            d.TWeekly // 1
            }).Select((t,i) => confirmingList.Contains(t) ? weights[i] : 0).Sum()
    });
NetMage
  • 26,163
  • 3
  • 34
  • 55