Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何编写Linq查询以执行向上包含?_C#_Sql Server_Linq_Entity Framework - Fatal编程技术网

C# 如何编写Linq查询以执行向上包含?

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

我正在使用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 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进行筛选

  • 主题.名称
  • 副标题.名称
我知道我可以把Linq写下来,因为我用它来得到答案。但是,我可以对我的Linq查询进行编码,以获得Topic.Name、SubTopic.Name和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();