C# 如何首先在实体框架代码中的linq中使用可空列表?

C# 如何首先在实体框架代码中的linq中使用可空列表?,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,我在筛选表中的状态列时遇到问题: public class QuestionInfo { [Key] public int Id { get; set; } public string Content { get; set; } public QuestionStatus Status { get; set; } } public enum QuestionStatus : byte { None = 0, NeedEdit = 1, B

我在筛选表中的状态列时遇到问题:

public class QuestionInfo
{
    [Key]
    public int Id { get; set; }
    public string Content { get; set; }
    public QuestionStatus Status { get; set; }
}

public enum QuestionStatus : byte
{
    None = 0,
    NeedEdit = 1,
    Blocked = 2,
    Accepted = 3,
    Checking = 4
}
我想通过可为空的状态列表筛选我的问题,当filterList为空时,我想选择all,例如:

List<QuestionStatus> filterList = new List<QuestionStatus>();

var questions = (from x in context.QuestionInfoes 
                 where filterList?.Contains(x.Status) 
                    && x.Score > 10 select x).ToList();
我在查询1中遇到以下语法错误:

C-无法隐式转换类型bool

我从查询2中得到此错误:

System.NotSupportedException:无法创建“System.Collections.Generic.List”1[[Atitec.OffseeAPI.DataBase.Models.Games.QuestionsOfKing.QuestionStatus,Atitec.OffseeAPI.DataBase,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]]类型的空常量值。 在此上下文中仅支持实体类型、枚举类型或基元类型

如何首先在实体框架代码中的linq中使用可空列表

正如您已经发现的,您不能不支持异常

解决方案是在以下情况下使用条件:

如何首先在实体框架代码中的linq中使用可空列表

正如您已经发现的,您不能不支持异常

解决方案是在以下情况下使用条件:

var questions = (from x in context.QuestionInfoes 
                 where (filterList == null ? true : filterList.Contains(x.Status))
                    && x.Score > 10 select x).ToList();
var query = context.QuestionInfoes.Where(x => x.Score > 10);
if (filterList != null)
    query = query.Where(x => filterList.Contains(x.Status));
var result = query.ToList();