C# 使用linq c筛选包含来自另一个表的对象的对象#

C# 使用linq c筛选包含来自另一个表的对象的对象#,c#,entity-framework,linq,C#,Entity Framework,Linq,我有一个看起来很简单的问题,但我无法解决它 我有两个表格,一个包含问题,一个包含回答。这些问题被映射在一起,使得一个问题有许多允许的答案。EntityFramework很好地处理了这个映射,所以当我调用控制器获取问题时,我会得到一组可爱的问题,这些问题包含了他们相关的回答 我们最近扩展了系统,在本例中包括两个用户组-A和B。有些问题和回答只对某些群体有效。因此,每个问题都有一个showToA showToB属性-使用简单的linq.where查询可以很好地工作。但是,我不知道如何使用参数show

我有一个看起来很简单的问题,但我无法解决它

我有两个表格,一个包含问题,一个包含回答。这些问题被映射在一起,使得一个问题有许多允许的答案。EntityFramework很好地处理了这个映射,所以当我调用控制器获取问题时,我会得到一组可爱的问题,这些问题包含了他们相关的回答

我们最近扩展了系统,在本例中包括两个用户组-A和B。有些问题和回答只对某些群体有效。因此,每个问题都有一个showToA showToB属性-使用简单的linq.where查询可以很好地工作。但是,我不知道如何使用参数showToGroupA调用getQuestions,并让它返回仅与指定组相关的问题和回答

我基本上希望能够得到所有相关的问题,并剔除任何不相关的回答

非常感谢您的帮助,谢谢

 public class Question
{
    [Key]
    public int ID { get; set; }
    public int QID { get; set; }
    public string question { get; set; }
    public string type { get; set; }
    public virtual List<AllowedResponses> AllowedResponse { get; set; }

    public int PrimaryOrderNo{ get; set; }
    public int SecondaryOrderNo{ get; set; }
    public bool ShowToGroupA{ get; set; }
    public bool ShowToGroupB{ get; set; }
}

//Model of allowed responses to questions
public class AllowedResponses
{
    [Key]
    public int ID { get; set; }
    public virtual int QID { get; set; }
    public string Response { get; set; }
    public int ResponseID { get; set; }
    public bool ShowToGroupA { get; set; }
    public bool ShowToGroupB { get; set; }
}
公开课问题
{
[关键]
公共int ID{get;set;}
公共int QID{get;set;}
公共字符串问题{get;set;}
公共字符串类型{get;set;}
公共虚拟列表允许响应{get;set;}
public int PrimaryOrderNo{get;set;}
public int SecondaryOrderNo{get;set;}
公共bool ShowToGroupA{get;set;}
公共bool ShowToGroupB{get;set;}
}
//允许回答问题的模型
公共类允许响应
{
[关键]
公共int ID{get;set;}
公共虚拟int QID{get;set;}
公共字符串响应{get;set;}
公共int响应ID{get;set;}
公共bool ShowToGroupA{get;set;}
公共bool ShowToGroupB{get;set;}
}
目前,我只是返回一个问题列表,按适当的顺序排序,并根据是否应向小组显示问题进行过滤,而不是过滤允许的回答

List<Questions> Questions = _repo.GetQuestions();
        Questions = Questions.OrderBy(x => x.GroupAOrderNo).ToList();
        List<Questions> QuestionsFiltered;
        if (GroupAorB == "A")
        {
            QuestionsFiltered = Questions.Where(a => a.ShowToA == true).ToList();
        } else
        {
            QuestionsFiltered = Questions.Where(a => a.ShowToB == true).ToList();
        }
        return Request.CreateResponse(HttpStatusCode.OK, Questions);
List Questions=\u repo.GetQuestions();
Questions=Questions.OrderBy(x=>x.GroupAOrderNo.ToList();
列出经过筛选的问题;
如果(GroupAorB==“A”)
{
QuestionsFiltered=Questions.Where(a=>a.ShowToA==true).ToList();
}否则
{
QuestionsFiltered=Questions.Where(a=>a.ShowToB==true).ToList();
}
返回请求.CreateResponse(HttpStatusCode.OK,问题);

请注意,我在这里简化了代码并更改了一些名称,请原谅由此导致的逻辑故障。

您可以在一个新对象中过滤掉您的响应,例如:-

List<Questions> Questions = _repo.GetQuestions();
    Questions = Questions.OrderBy(x => x.GroupAOrderNo).ToList();
var  FilteredQuestions =
                Questions.Where(a => GroupAorB == "A" ? a.ShowToGroupA : a.ShowToGroupB).Select(q => new Question
                {
                    ID = q.ID,
                    AllowedResponse =
                        q.AllowedResponse.Where(r => GroupAorB == "A" ? r.ShowToGroupA : r.ShowToGroupB).ToList()

                }).ToList();
return Request.CreateResponse(HttpStatusCode.OK, Questions);
List Questions=\u repo.GetQuestions();
Questions=Questions.OrderBy(x=>x.GroupAOrderNo.ToList();
变量筛选问题=
问题。其中(a=>GroupAorB==“a”?a.ShowToGroupA:a.ShowToGroupB)。选择(q=>new Question
{
ID=q.ID,
允许响应=
q、 AllowedResponse.Where(r=>GroupAorB==“A”?r.ShowToGroupA:r.ShowToGroupB).ToList()
}).ToList();
返回请求.CreateResponse(HttpStatusCode.OK,问题);

不起作用的代码在哪里?因此,如果我理解正确,可能会有一个标记为
ShowToGroupA
的问题,其答案标记为
ShowToGroupB
?是的,这是正确的,因为问题和答案是两个独立的实体。将来我们可能会决定向不同的群体展示不同的问题。