我正在使用实体框架执行以下查询,我想通过作为TakerStatusEnum筛选结果:

var takerCount = this.DbContext.Set<Quiz>().Single(x => x.UrlId == urlId).Takers.Count(x => x.TakerStatus == TakerStatus.Abandoned || x.TakerStatus == TakerStatus.Complete)


这是enum

public enum TakerStatus
{
    /// <summary>
    /// Taker is currently in the process of completing the quiz
    /// </summary>
    InProgess = 0,

    /// <summary>
    /// Taker failed to complete the quiz and was marked as abandoned
    /// </summary>
    Abandoned = 1,

    /// <summary>
    /// Taker successfully completed taking the quiz
    /// </summary>
    Complete = 2
}


我意识到此查询运行缓慢,我想知道为什么其中存在很多记录,但是我想通过添加索引来提高速度。

但是在检查了实际由此产生的查询之后,我发现它似乎忽略了lambda表达式...

SELECT
`Extent1`.`Id`,
`Extent1`.`QuizId`,
`Extent1`.`QuizVersionId`,
`Extent1`.`UserId`,
`Extent1`.`TakerName`,
`Extent1`.`CurrentQuestionNumber`,
`Extent1`.`CurrentQuestionStartTime`,
`Extent1`.`CurrentScore`,
`Extent1`.`Completed`,
`Extent1`.`IpAddress`,
`Extent1`.`StartTime`,
`Extent1`.`FinishTime`,
`Extent1`.`Abandoned`,
`Extent1`.`PasswordId`,
`Extent1`.`PersonalityResultId`,
`Extent1`.`PercentageResult`,
`Extent1`.`Status`,
`Extent1`.`TakerStatus`
FROM `Taker` AS `Extent1`
 WHERE `Extent1`.`QuizId` = 330


它选择了每个参加测验的人,甚至没有检查TakerStatus列。

是什么赋予了?我想向TakerStatus添加索引以加快速度

最佳答案

试试这个,我想它需要在条件周围加一副大括号。我认为这是一个运算符优先级问题。

var takerCount = this.DbContext.Set<Quiz>().Single(x => x.UrlId == urlId).Takers.Count(x => (x.TakerStatus == TakerStatus.Abandoned || x.TakerStatus == TakerStatus.Complete))

09-11 19:51