加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 我怎么能.在LINQ中包含多个级别?

发布时间:2020-12-15 08:35:24 所属栏目:百科 来源:网络整理
导读:我有以下课程: public class Problem : AuditableTable{ public Problem() { this.Questions = new ListQuestion(); } public int ProblemId { get; set; } public string Title { get; set; } public virtual ICollectionQuestion Questions { get; set; }
我有以下课程:
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; }
}

我想发出这样的查询:

var problems = _problemsRepository.GetAll()
            .Where(p => p.ProblemId == problemId)
            .Include(p => p.Questions)
            .Include(p => p.Questions.Answers)
            .ToList();
        return problems;

所以我可以看到问题,问题和答案信息.但我的最后一个包含有一个问题,我无法弄清楚如何包含答案.

有人可以给我一些建议.

解决方法

您可以使用.Select().
var problems = _problemsRepository.GetAll()
            .Where(p => p.ProblemId == problemId)
            .Include(p => p.Questions.Select(q => q.Answers))
            .ToList();

现在你的答案将被包括在内.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读