How do I make parameter that will accept (x => x.Value) like .Sum(x => x.Value) or .Average(x => x.Value). I'm thinking I need it to be a Func<> but I'm not sure. I don't know what the method signature should look.
Here's what my current extension method looks like:
public static double StdDev(this IEnumerable<double> values)
{
//code
}
Here's my overload extension so far based (I know this isn't right):
public static double StdDev(this IEnumerable<double> values, Func<double, bool> filter)
{
IEnumerable<double> data;
if (filter != null)
{
data = values.Where(filter);
}
else
{
data = values;
}
return data.StdDev();
}
Here's my unit test:
[TestClass]
public class IEnumerableExtensions_StdDev
{
[TestMethod]
public void StdDev_Success_Lambda()
{
var dataSet = new List<TestType> {new TestType(35.2), new TestType(54.43)};
//This Line compiles fine but the following one does not.
var test = dataSet.Sum(x => x.Value);
var stdev = dataSet.StdDev(x => x.Value);
stdev.Should().BeInRange(13.5, 13.6);
}
}
public class TestType
{
public double Value { get; set; }
public TestType(double value)
{
Value = value;
}
}
The unit test does not compile. It gives me an error that reads:
'List' does not contain a definition for 'StdDev' and the best extension method overload 'IEnumerableExtensions.StdDev(IEnumerable, Func<double, bool>)' requires a receiver of type 'IEnumerable'
I don't want to change the test I want to change the overload method signature and/or body but I don't understand what I need to change in my code to make this work.