简体   繁体   中英

Add a collection to a view model List

I have a view model like

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

and I have a query to database like

var query = from p in data.GetMember 
           where (p.Client == client && p.Person_id == pid) 
           select p;

this query returns two fields: long person_id , int comm_id which are same in the view model except that the view model has one more field: someclass cls

How can I need add the results returned by the query to my view model?

Output should be a list which contains collection of memberCommunication and a null-valued cls collection for each collection of memberCommunication.

I think you want to do something like this:

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

This will give you a query which is of type IEnumerable<MemberCommunicationType> .

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

You can then convert it to a List<T> with query.ToList() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM