开发者

LINQ Filter and Convert C# .NET 4.0

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜