I have an abstract class called Grouping. I have a subclass called GroupingNNA.
public class GroupingNNA : Grouping {
// blah blah blah
}
I have a List that contains items of type GroupingNNA, but is actually declared to contain items of type Grouping.
List<Grouping> lstGroupings = new List<Grouping>();
lstGroupings.Add(
new GroupingNNA { fName = "Joe" });
lstGroupings.Add(
new GroupingNNA { fName = "Jane" });
The Problem:
The following LINQ query blows up on me because of the fact that lstGroupings is declared as List< Grouping> and fName is a property of GroupingNNA, not Grouping.
var results = from g in lstGroupings
where r.fName == "Jane"
select r;
Oh, and this is a compiler error, not a runtime error. Thanks in advance for any help on this one!
More Info:
Here is the actual method that won’t compile. The OfType() fixed the LINQ query, but the compiler doesn’t like the fact that I’m trying to return the anonymous type as a List< Grouping>.
private List<Grouping> ApplyFilterSens(List<Grouping> lstGroupings, string fSens) {
// This works now! Thanks @Lasse
var filtered = from r in lstGroupings.OfType<GroupingNNA>()
where r.QASensitivity == fSens
select r;
if (filtered != null) {
**// Compiler doesn't like this now**
return filtered.ToList<Grouping>();
}
else
return new List<Grouping>();
}
Try:
this will skip any elements not of type
GroupingNNA, and also make the compiler useGroupingNNAas the type forg.In response to comment and edited question. No, the compiler will certainly not be happy about your changed collection, but you can fix that:
This relies on the fact that arrays in .NET are co/contra-variant, which allows the compiler to treat
GroupingNNA[]asGrouping[]for the constructor.Also, you don’t need the
if (filtered != null)check, you will get a collection infiltered, it might just not produce any elements, butfilteredwill always be non-null.This means your code can be written as:
or even just:
or even shorter if you drop the linq syntax:
Note that you can of course use
OfTypeto go the other way as well:there shouldn’t be any big performance differences between the different ways here, if in doubt, measure it, but I would go with what you find easiest to read and understand.