将 Linq.Contains 方法添加到表达式 Func<Object,Bool>

Add Linq.Contains method To a Expression Func<Object,Bool>

我对屏幕截图中的黄色部分有疑问。我想要如果 SelectedGroup 不为空。除了添加到该表达式的术语之外,添加另一个条件。就是这样。

c => SelectedGroup.Contains (c.CourseGroup.Id) 

Select 所有那些 ID 在该范围内的 SelectedGroups。我写的这个查询没有用。给出的错误是它无法转换为 SQL 查询。我不知道要解决这个问题。请帮助我

enter image description here

有关我的代码的更多详细信息 - 这是我的代码存储库:

enter link description here

enter link description here

搭上 combine Func,它帮不了你。如果你真的打算 return 一个可查询的,你不应该编译你的表达式。

你可以做的是:

Expression<Func<Course, bool>> expression = c => (string.IsNullOrEmpty(Title) || EF.Functions.Like(c.CourseTitle, $"%{Title}%")
        && c.IsDeleted == IsDeleted);

        switch (statusType)
        {
            case PriceStatusType.All:
                break;
            case PriceStatusType.Free:
                queryable = queryable.Where(expression).Where(c => c.CoursePrice < 1000);
                break;
            case PriceStatusType.Cash:
                queryable = queryable.Where(expression).Where(c => c.CoursePrice <= MaxPrice && c.CoursePrice >= MinPrice);
                break;
        }
        if (SelectedGroup != null && SelectedGroup.Any())
        {
            Expression<Func<Course, bool>> e = c => SelectedGroup.Contains(c.CourseGroup.Id);
            queryable = queryable.Where(expression);
            queryable = queryable.Where(c => SelectedGroup.Contains(c.CourseGroup.Id));


        }

        return queryable;

此外,我删除了 Include,因为它没有用,如果需要,应该由调用者使用。