I am trying to simplify with linq and hopefully make cheaper an expression that takes 2 lists of numbers and subtracts the first from the second. I have something that works, but I think it can be cleaner and more efficient.
double[] main = _mainPower.Select(i => i.Decibels).ToArray(); double[] reference = _referencePower.Select(i => i.Decibels).ToArray(); List<double> amplitudeList = new List<double>(); for (int i = 0; i < main.Count(); i++) { if (!double.IsNaN(main[i] - reference[i])) { amplitudeList.Add(main[i] - reference[i]); } } return amplitudeList;
If I have 2 lists, List1 = {8,5,3} and List2 = {5,2,1}, then the returned list will be {3,3,2} I tried
return _mainPower.Select(i => i.Decibels - _referencePower.Select(a => a.Decibels));
but it obviously does not work. Is there a way to turn my function into a good linq query? One thing that I did not allow is a list of two different sizes. If the sizes are different, then the longer list should be cut off from the end to make them the same as the smaller one.
Any help would be greatly appreciated.
Thanks,
- EDIT -
Thanks for the help, I used a message from StriplingWarrior to get what I need.
_mainPower.Zip(_referencePower, (v1, v2) => v1.Decibels - v2.Decibels).Where(i => !double.IsNaN(i));
source share