2015-10-13 44 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