使用Linq选择类的属性以返回IEnumerable
如果我有一个SortedList
,我想从该类返回一个新的IEnumerable
属性,我该怎么做?
我尝试过SortedList.Select(x=>x.MyProperty, x.AnotherProperty)
但它不起作用。
谢谢。
您可以返回一个匿名对象:
var result = SortedList.Select(x => new { x.Value.MyProperty, x.Value.AnotherProperty });
或者,如果要使用当前方法范围之外的结果,可以定义自定义类型:
IEnumerable result = SortedList.Select(x => new MyType { Prop1 = x.Value.MyProperty, Prop2 = x.Value.AnotherProperty });