webdad3
webdad3

Reputation: 9080

Adding list values to a LINQ query

I have the following object (view model) that I want to use:

public class assignmentViewModel 
{
    public string choreName { get; set; }
    public List<string> personName { get; set; }

}

LINQ statement:

            var avm = (from a in Assignments
                       join c in Chores on a.ChoreID equals c.ChoreId
                       join p in Persons on a.PersonID equals p.PersonID
                       select new assignmentViewModel
                       {
                           personName = p.PersonName.ToList(),
                           choreName = c.ChoreName
                       }).ToList();

I can have multiple people in an assignment. I want to be able to pull back my data into this ViewModel. The error I'm getting currently is:

Cannot implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<string>'

My data (if it helps) is:

chore #1
person 1

chore #2
person 1 
person 2

The Person model is:

public partial class person
{
    public int personID { get; set; }
    public string personName { get; set; }
    public System.DateTime personDOB { get; set; }
    public string personEmail { get; set; }
    public string personPhone { get; set; }
    public string personPhoneCarrier { get; set; }
    public bool isActive { get; set; }
}

Upvotes: 0

Views: 104

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101681

You are looking for Grouping here, you need to group the records based on choreName, I would do it like this:

(from a in Assignments
 join c in Chores on a.ChoreID equals c.ChoreId
 join p in Persons on a.PersonID equals p.PersonID
 select new 
        {
            person = p,
            choreName = c.ChoreName
        })
  .GroupBy(x => x.choreName)
  .Select(g => new assignmentViewModel
               {
                   personName = g.Select(x => x.person.PersonName).ToList()
                   choreName =  g.Key
               }).ToList();

Upvotes: 2

Related Questions