2016-12-06 130 views
1

我有兩個類對象的數據viewModel。C#從ViewModel獲取類對象列表

public class StudentViewModel 
{ 
    public PeopleEntity People { get; set; } 
    public PeopleUnitEntity PeopleUnit { get; set; } 
} 

在下面的代碼,我得到兩個PeopleEntity和PeopleUnitEntity

IList<StudentViewModel> _stv = _StudentServicesObject.GetStudentByPersonCode(PersonCode); 

我想存儲只有人名單新PeopleEntity對象的值,我該怎麼辦在於:

我嘗試如下,但它不爲我工作

IList<PeopleEntity> _People = _StudentServicesObject.GetStudentByPersonCode(PersonCode).Select(pl => new PeopleEntity { People = pl }); 

錯誤

enter image description here

+0

爲什麼不能.ToList() –

回答

2

豈不只是:

IList<PeopleEntity> _People = _StudentServicesObject.GetStudentByPersonCode(PersonCode).Select(pl => pl.People).ToList(); 

???

+0

是的我試過了,在屏幕截圖 – toxic

+0

中添加.ToList()到最後。 – dviljoen

+0

或者將_stv的定義更改爲IEnumerable – dviljoen

0

其中返回IEnumerable而不是IList,因此您需要將_stv設置爲IEnumerable或將結果轉換爲List。

+0

所以,你的意思是說已經說過的那個 –

+0

對不起,當我提供這個答案那裏沒有答案在評論中給出! –