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

本文关键字:模型 列表 视图 集合 添加 | 更新日期: 2023-09-27 17:57:41

我有一个类似的视图模型

    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_idint comm_id,这两个字段在视图模型中是相同的,只是视图模型多了一个字段someclass cls

如何将查询返回的结果添加到视图模型中?

输出应该是一个包含memberCommunication集合的列表,以及每个memberCommunications集合的空值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>