I found some duplicate code today and would like to reduce it to one method. In order to do it, I'd like to inject something more abstract into the lambda here:
public IEnumerable<AbstractFoo> GetMatchingFoos()
{
return IEnumerable<AbstractFoo> exactMatchFoo = exactMatchList
.Where (d => d is RedFoo);
}
//Horrifying duplicate code!:
public IEnumerable<AbstractFoo> GetMatchingFoos()
{
return IEnumerable<AbstractFoo> exactMatchFoo = exactMatchList
.Where (d => d is BlueFoo);
}
I'd like to be able to replace RedFoo
/ BlueFoo
with something I can inject into a single method like this:
public IEnumerable<AbstractFoo> GetMatchingFoos(paramFoo)
{
IEnumerable<AbstractFoo> exactMatchFoo = exactMatchList
.Where (d => d is paramFoo.GetType()); //compile error
}
I tried using curly braces to access the local variable paramFoo, but that doesn't compile.
IEnumerable<AbstractFoo> exactMatchFoo = exactMatchList
.Where (d => is {paramFoo.GetType();}); //compile error
Also of note: AbstractFoo
is an abstract class, that both RedFoo
and BlueFoo
inherit from. No interfaces in my code at this point.
How can the type of a local variable be captured inside a lambda expression in linq?