2015-10-13 45 views
2

嵌套类我有一个模型类,如:选择数据与LINQ

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 to从数据库中收集数据:

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 
      }; 

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

+2

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

回答

1

我假设你有XY属性t。您只需初始化Coordinate对象并使用object initializer来设置XcoorYcoor属性。与您的操作相同SectionCoordinateViewModel

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

注意:尝试改进变量的命名。例如。而不是section,你应该使用sections,因为你得到了服务的所有部分。不是单一的。您可以使用s代替t,代表section的第一个字母。而不是data你可以使用类似models。您在坐标属性中也不需要coor后缀。顺便说一句,可能Point可能是更适合Coordinate类的名称。

+1

谢谢你的工作:D,并感谢您的建议 – Mamen