Would like to both filter and convert a List. I can filter but cannot figure out how to convert. PROBLEM, the return statement returns a List of FieldDef. How do I get a List of the FieldDefsEnum1 from fieldsDefs? Thanks in advance.
FieldDefEnum1 : FieldDef
List<FileDef> fieldDefs
public List<FieldDefEnum1> FieldDefsEnum1
{
get
{
return FieldDefs.Where(fd => fd.GetType() == t开发者_开发技巧ypeof(FieldDefEnum1)).ToList();
}
}
You can use OfType<T>()
instead:
return FieldDefs.OfType<FieldDefEnum1>().ToList();
FieldDefs.OfType<FieldDefEnum1>().ToList();
To convert arbitrarily ("map") between values, use Select()
.
OfType()
probably does the filtering and converting you need more succintly - although it will include subclasses of the given type, not just instances of that specific type.
精彩评论