Alan2
Alan2

Reputation: 24562

How can I use LINQ to populate a collection inside a collection?

I have the following LINQ code that I created:

 QuestionDetail questions = _questionsRepository
                .GetAll()
                .Include(q => q.Answers)
                .Select(m => new QuestionDetail
                {
                    QuestionId = m.QuestionId,
                    Text = m.Text,
                    Answers // << I am not sure how to populate this
                            // << I need to have a new collection 
                            // << created from a subset of the m.Answers
                })
                .FirstOrDefault();

My problem is that I am not sure how to populate the ICollection<AnswerDetail> Answers collection that is part of the QuestionDetail. What I need is to somehow select from the m.Answers and use this to populate the AnswerDetail collection. I know I cannot use new AnswerDetail as Answers is a collection.

Can anyone help and tell me how I could do this.

Below I have listed some of the classes for this. To make it simpler I removed some fields from the Question and Answer classes.

public class QuestionDetail
{
    public int QuestionId { get; set; }
    public string Text { get; set; }
    public virtual ICollection<AnswerDetail> Answers { get; set; }
}

public class AnswerDetail
{
    public int AnswerId { get; set; }
    public string Text { get; set; }
}

public class Answer
{
    public int AnswerId { get; set; }
    public int QuestionId { get; set; }
    public Nullable<bool> Correct { get; set; }
    public Nullable<bool> Response { get; set; }
    public string Text { get; set; }
    public virtual Question Question { get; set; }
}

public class Question
{
    public Question()
    {
        this.Answers = new List<Answer>();
    }
    public int QuestionId { get; set; }
    public string Text { get; set; }
    public string Image { get; set; }
    public virtual ICollection<Answer> Answers { get; set; }
}

Upvotes: 1

Views: 1728

Answers (3)

Tommy Grovnes
Tommy Grovnes

Reputation: 4156

Try this:

Take(1) instead of FirstOrDefault project result on the "outside":

QuestionDetail questions = _questionsRepository
        .GetAll()
        .Include(q => q.Answers)
        .Take(1)
        .ToList()
        .Select(m => new QuestionDetail
        {
            QuestionId = m.QuestionId,
            Text = m.Text,
            Answers = m.Answers.Select(a => 
                              new AnswerDetail { AnswerId = a.AnswerId, 
                                                 Text = a.Text }).ToList()
        });

Upvotes: 1

Alessandro D&#39;Andria
Alessandro D&#39;Andria

Reputation: 8868

For what I can see this should work:

Answers = m.Answers.Select(a => 
              new AnswerDetail { AnswerId = a.AnswerId, 
                                 Text = a.Text }).ToList(),

You have a list of Answer and transform them to a list of AnswerDetail.

Upvotes: 2

No Idea For Name
No Idea For Name

Reputation: 11577

if you need a subset of q.Answers and you have a condition you can do:

QuestionDetail questions = _questionsRepository
            .GetAll()
            .Include(q => q.Answers)
            .Select(m => new QuestionDetail
            {
                QuestionId = m.QuestionId,
                Text = m.Text,
                Answers = m.Answers.Where(x=>yourCondition)
            })
            .FirstOrDefault();

Upvotes: 1

Related Questions