我有什么看起来像一个简单的问题,但不能包裹我的头。使用linq c过滤包含另一个表中的对象的对象#
我有两个表格,一个包含问题,一个答复。这些映射在一起,使得一个问题有许多allowedResponses。 EntityFramework很好地处理了这个映射,所以当我将控制器调用到GetQuestions时,我会回复一系列可爱的问题,其中包含相应的响应。
我们最近扩展了系统以包含两个用户组 - 在本例中为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; }
}
目前,我只是返回的问题清单,通过适当的排序,对这个问题是否应该被显示到组过滤 - 不过滤AllowedResponses。
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);
请注意我在这里简化了代码并更改了一些名称,请原谅在逻辑中导致的任何故障。
哪里的代码不能正常工作? –
因此,如果我理解正确,可以将标记为ShowToGroupA的问题标记为ShowToGroupB。 – grek40
是的,这是正确的,因为问题和答案是两个单独的实体。将来我们可能会决定向不同的群体展示不同的问题。 – anthonyhumphreys