[英]Create a List<T> with T being an anonymous type using LINQ in C#
Let's say we have the List<Item> data
, and the Item
class is defined as below: 假设我们有
List<Item> data
,并且Item
类的定义如下:
public class Item
{
public double Station { get; set; }
public double Area { get; set; }
...
// other properties present in the class
}
Now how can we create a new List<T>
out of List<Item>
, so that T
is anonymous type having only double Station
and double Area
as its properties. 现在,我们如何从
List<Item>
创建一个新的List<T>
,以使T
是仅具有double Station
和double Area
作为其属性的匿名类型。
I know how to extract all of the station and area values into a List<double> data2
but what I want above is different. 我知道如何将所有测站和区域值提取到
List<double> data2
但是我上面想要的是不同的。
var data2 = data
.SelectMany(x => new[] { x.Station, x.Area })
.ToList();
I think what you need is... 我认为您需要的是...
var data2 = data
.Select(x => new { x.Station, x.Area })
.ToList();
You don't need SelectMany
here, usually it is used to flatten the hierarchy. 您在这里不需要
SelectMany
,通常它用于展平层次结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.