C# 使用LINQ选择要嵌套类的数据

C# 使用LINQ选择要嵌套类的数据,c#,linq,C#,Linq,我有一个模型课,比如: public class Coordinate { public decimal Xcoor { get; set; } public decimal Ycoor { get; set; } } 然后我还有一节课: public class SectionCoordinateViewModel { public SectionCoordinateViewModel() { this.Coordinate = new Coo

我有一个模型课,比如:

public class Coordinate
{
    public decimal Xcoor { get; set; }
    public decimal Ycoor { get; set; }
}
然后我还有一节课:

public class SectionCoordinateViewModel
{
    public SectionCoordinateViewModel()
    {
        this.Coordinate = new Coordinate();
    }
    public string SectionId { get; set; }
    public Coordinate Coordinate { get; set; }
}
然后我使用LINQ从db收集数据:

var section = sectionService.getAll();
var data = from t in section
           select new SectionCoordinateViewModel
           {
               SectionId = "section_" + t.Id,
               //how to send data to Coordinate.Xcoor and Coordinate.Ycoor
           };

如何将它们发送到坐标?谢谢

我假设您在
t
中有
X
Y
属性。您只需初始化
Coordinate
对象并使用设置
Xcoor
Ycoor
属性。与您对
部分协调设备模型所做的操作相同:

var data = from t in section
           select new SectionCoordinateViewModel
           {
               SectionId = "section_" + t.Id,
               Coordinate = new Coordinate
               {
                  Xcoor = t.X,
                  Ycoor = t.Y
               }
            };

注意:尝试改进变量的命名。例如,您应该使用
章节
,而不是
章节
,因为您可以从服务中获得所有章节。一个也没有。您可以使用
s
代替
t
,它代表
部分的第一个字母。您可以使用类似于
模型的东西来代替
数据
。在坐标的属性中也不需要
coor
后缀。顺便说一句,
可能是
坐标
类更合适的名称。

什么是
t
?你将在哪里得到X和Y?