【问题标题】:C# Get Class Object List from ViewModelC# 从 ViewModel 获取类对象列表
【发布时间】:2016-12-06 15:28:09
【问题描述】:

我有两个类对象的数据视图模型。

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

错误

【问题讨论】:

  • 为什么不 .ToList()

标签: c# viewmodel


【解决方案1】:

不就是这样吗:

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

???

【讨论】:

  • 是的,我试过了,在我的问题更新中用截图参考错误
  • 将 .ToList() 添加到末尾。
  • 或者将_stv的定义改为IEnumerable
【解决方案2】:

Where 返回 IEnumerable,而不是 IList,因此您需要将 _stv 设为 IEnumerable 或将结果转换为 List。

【讨论】:

  • 对不起,当我在 cmets 中没有给出答案的情况下提供这个答案时!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多