我有一个模型类:
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 从数据库中收集数据:
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
对象并使用 object initializer设置 Xcoor
和 Ycoor
属性。与您对 SectionCoordinateViewModel
所做的相同:
var data = from t in section
select new SectionCoordinateViewModel
{
SectionId = "section_" + t.Id,
Coordinate = new Coordinate
{
Xcoor = t.X,
Ycoor = t.Y
}
};
注意:尝试改进变量的命名。例如。你应该使用 sections
而不是 section
,因为你从服务中获取所有部分。不是一个人。您可以使用 s
代替 t
,它代表 section
的第一个字母。您可以使用 models
之类的东西代替 data
。您也不需要 coor
坐标属性中的后缀。顺便说一句,Point
可能更适合 Coordinate
类的名称。
关于c# - 使用 LINQ 选择数据到嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33098617/