gpt4 book ai didi

linq过滤子集合

转载 作者:行者123 更新时间:2023-12-02 00:32:24 25 4
gpt4 key购买 nike

我已经阅读了很多关于此的不同主题,但我还没有找到我要找的东西。

我有一个 EF 查询是这样的:

var query = this.ObjectContext.Questions
.Include("AnswerKey").Include("QuestionTypes")
.Where(o => o.SurveyQuestions.Any(o2 => o2.SurveyID == id));

在我意识到我没有考虑 AnswerKey 子集合的 Active Flag 之前,它工作正常。换句话说,此查询应加载所有父调查 ID 为 3(确实如此)的问题,但仅加载事件标志为 true 的 AnswerKeys。

我试过这个:

var query = this.ObjectContext.AnswerKey
.Include("Questions.QuestionTypes")
.Where(ak =>
ak.Active == true &&
ak.Questions.SurveyQuestions.Any(sq => sq.SurveyID == 3) &&
ak.Questions.Active == true)
.AsEnumerable()
.Select(ak => ak.Questions).AsQueryable();

但它会为每个答案键返回 1 个问题。因此,如果一个问题有 4 个答案,它会出现 4 次...

我该怎么做?

最佳答案

您可以在最后使用 Distinct() 来过滤掉重复项:

  .AsEnumerable()
.Select(ak => ak.Questions)
.Distinct()
.AsQueryable();

关于linq过滤子集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6205154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com