如何解决将集合添加到视图模型列表中
| 我有一个像这样的视图模型 public class MemberCommunicationType
{
public long person_id { get; set; }
public int comm_id { get; set; }
public someclass cls{ get; set; }
}
public class GetMemberColl
{
public MemberCommunicationType[] memberCommunication{ get; set; }
}
我有一个查询数据库
var query = from p in data.GetMember
where (p.Client == client && p.Person_id == pid)
select p;
该查询返回两个字段:long person_id
,int comm_id
,它们在视图模型中相同,但视图模型还有一个字段:someclass cls
我如何需要将查询返回的结果添加到我的视图模型中?
输出应该是一个列表,其中包含memberCommunication的集合以及每个memberCommunication集合的空值cls集合。
解决方法
我认为您想做这样的事情:
var query = from p in data.GetMember
where (p.Client == client && p.Person_id == pid)
select new MemberCommunicationType
{ person_id = p.person_id,comm_id = p.comm_id}
;
var output = new GetMemberColl { memberCommunication = query.ToArray() };
,这将为您提供类型为IEnumerable<MemberCommunicationType>
的query
。
var query = from p in data.GetMember
where (p.Client == client && p.Person_id == pid)
select new MemberCommunicationType
{
person_id = p.Person_id,comm_id = p.comm_id,cls = null
};
然后,您可以将其转换为带有query.ToList()
的List<T>
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。