Gokul
Gokul

Reputation: 1371

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 .

Upvotes: 0

Views: 562

Answers (2)

Ben Straub
Ben Straub

Reputation: 5776

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().

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

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

Upvotes: 1

Related Questions