C# 如何编写Linq查询以执行向上包含?
我正在使用Entity Framework 5/SQL Server 2012,我有以下类:C# 如何编写Linq查询以执行向上包含?,c#,sql-server,linq,entity-framework,C#,Sql Server,Linq,Entity Framework,我正在使用Entity Framework 5/SQL Server 2012,我有以下类: public partial class Topic { public int TopicId { get; set; } public string Name { get; set; } public int SubjectId { get; set; } public virtual Subject Subject { get; set; } public v
public partial class Topic {
public int TopicId { get; set; }
public string Name { get; set; }
public int SubjectId { get; set; }
public virtual Subject Subject { get; set; }
public virtual ICollection<SubTopic> SubTopics { get; set; }
}
public partial class SubTopic {
public int SubTopicId { get; set; }
public string Name { get; set; }
public int TopicId { get; set; }
public virtual Topic Topic { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
public class Question {
public int QuestionId { get; set; }
public int QuestionStatusId { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public int SubTopicId { get; set; }
public virtual SubTopic SubTopic { get; set; }
public virtual ICollection<Answer> Answers { get; set; }
}
公共部分类主题{
公共int-TopicId{get;set;}
公共字符串名称{get;set;}
public int SubjectId{get;set;}
公共虚拟主题主题{get;set;}
公共虚拟ICollection子主题{get;set;}
}
公共部分类副标题{
公共整型子主题ID{get;set;}
公共字符串名称{get;set;}
公共int-TopicId{get;set;}
公共虚拟主题主题{get;set;}
公共虚拟ICollection问题{get;set;}
}
公开课问题{
public int QuestionId{get;set;}
public int QuestionStatusId{get;set;}
公共字符串标题{get;set;}
公共字符串文本{get;set;}
公共整型子主题ID{get;set;}
公共虚拟子主题子主题{get;set;}
公共虚拟ICollection答案{get;set;}
}
我使用以下方法获取问题详细信息:
public IList<Question> GetQuestionsUser(int userId, int questionStatusId) {
var questions = _questionsRepository.GetAll()
.Include(a => a.Answers)
.ToList();
return questions;
}
public IList GetQuestionsUser(int userId,int questionStatusId){
var questions=\u questions respository.GetAll()
.包括(a=>a.答案)
.ToList();
回答问题;
}
现在我还想返回以下两个字段,并按SubjectId进行筛选
- 主题.名称
- 副标题.名称
//假设您的repo GetAll()返回一个DbQuery
//assuming your repo GetAll() returns a DbQuery<T>
var questions = _questionsRepository.GetAll()
.Where(q=>q.SubTopic.Topic.SubjectId = mySubjectId)
.Include(q=>q.Answers)
.Include(q=>q.SubTopic.Topic)
.ToList();
var questions=\u questions respository.GetAll()
.其中(q=>q.SubTopic.Topic.SubjectId=mySubjectId)
.包括(q=>q.答案)
.Include(q=>q.SubTopic.Topic)
.ToList();