我怎样才能.INCLUDE倒在LINQ多个级别? [英] How can I .Include down more than one level in LINQ?
本文介绍了我怎样才能.INCLUDE倒在LINQ多个级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下类:
public class Problem : AuditableTable
{
public Problem()
{
this.Questions = new List<Question>();
}
public int ProblemId { get; set; }
public string Title { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
public Question()
{
this.Answers = new List<Answer>();
}
public int QuestionId { get; set; }
public int ProblemId { get; set; }
public virtual ICollection<Answer> Answers { get; set; }
public virtual Problem Problem { get; set; }
}
public class Answer : AuditableTable
{
public int AnswerId { get; set; }
public int QuestionId { get; set; }
public string Text { get; set; }
public virtual Question Question { get; set; }
}
我要发出这样的查询:
I want to issue a query like this:
var problems = _problemsRepository.GetAll()
.Where(p => p.ProblemId == problemId)
.Include(p => p.Questions)
.Include(p => p.Questions.Answers)
.ToList();
return problems;
所以我能看到问题,答问的信息。但是,有一个与我的最后一个问题包括,我无法工作,如何得到答案包括在内。
So I can see Problem, Question and Answer information. But there is a problem with my last include and I cannot work out how to get the Answers included.
有人可以给我一些这方面的建议。
Can someone give me some advice on this.
推荐答案
您。可以使用。选择()
You can use .Select().
var problems = _problemsRepository.GetAll()
.Where(p => p.ProblemId == problemId)
.Include(p => p.Questions.Select(q => q.Answers))
.ToList();
现在你的答案将包括在内。
Now your answers will be included.
这篇关于我怎样才能.INCLUDE倒在LINQ多个级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文