Add a collection to a view model List

69
Views
1
Answers

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 .

edit | flag
William Patrick
Asked on: Jun 16, 2011 at 9:17PM

1 Answers

22
4
22

Try using Anonymous type New{} in your query 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() };
edit | flag
Gokul A
Answered on: Jun 16, 2011 at 9:19PM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags