简体   繁体   中英

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

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 .

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

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

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