将集合添加到视图模型列表中

问题描述

| 我有一个像这样的视图模型
    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>
。