开发者

Using Linq to Select properties of class to return IEnumerable<T>

开发者 https://www.devze.com 2023-02-08 04:02 出处:网络
If I have a SortedList<int, MyClass开发者_StackOverflow中文版> and I want to return a new IEnumerable<T> of properties from that class how do I do that?

If I have a SortedList<int, MyClass开发者_StackOverflow中文版> and I want to return a new IEnumerable<T> of properties from that class how do I do that?

I have tried SortedList.Select(x=>x.MyProperty, x.AnotherProperty) but it doesnt work.

Thanks.


You could return an anonymous object:

var result = SortedList.Select(x => new { 
    x.Value.MyProperty, 
    x.Value.AnotherProperty 
});

Or if you want to use the result outside of the scope of the current method you could define a custom type:

IEnumerable<MyType> result = SortedList.Select(x => new MyType { 
    Prop1 = x.Value.MyProperty, 
    Prop2 = x.Value.AnotherProperty 
});
0

精彩评论

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

关注公众号