I am struggling with the paramaters for my IEnumerable<ISeries> Function
.
I created an interface AllProperties
, which combines all the properties from different classes that I am going to need at some point.
This is my IEnumerable<ISeries> GetDepartments
right now:
public IEnumerable<ISeries> GetDepartments()
{
IEnumerable<ISeries> series = Employees.GroupBy(x => x.Department).ToList().Select(x => new PieSeries<double>
{
Values = new List<double> { x.Count() },
Name = x.Key,
});
return series;
}
It works perfectly fine, but I'll have to copy/paste the code for each List and Property and I would like to avoid that.
The idea of the function I am trying to create is, that I reference a List
and a given property and return an IEnumerable<ISeries>
This is what I got so far:
public IEnumerable<ISeries> GetEnumerableGroupBy<T,TKey>(List<T> list, Func<AllProperties, TKey> myGroupingProperty)
{
IEnumerable<ISeries> series = list.GroupBy(myGroupingProperty).ToList().Select(x => new PieSeries<double>
{
Values = new List<double> { x.Count() },
Name = x.ToString(),
});
return series;
}
//Calling the function like this
GetEnumerableGroupBy(Employees, x => x.Department);
now I am getting the error, that List<T>
does not contain a definition for GroupBy
and I don't know how to fix this.
Any help would be greatly appreciated.
CodePudding user response:
You're very close. You don't need the AllProperties
interface at all. Instead, just make your function argument the generic T
type.
public IEnumerable<ISeries> GetEnumerableGroupBy<T, TKey>(List<T> list, Func<T, TKey> myGroupingProperty)
{
...
}
The rest of your code should work just fine.