【问题标题】:Add Linq.Contains method To a Expression Func<Object,Bool>将 Linq.Contains 方法添加到表达式 Func<Object,Bool>
【发布时间】:2019-05-03 17:10:20
【问题描述】:
【问题讨论】:
标签:
entity-framework
linq
lambda
expression
ef-core-2.2
【解决方案1】:
搭上combine Func,它帮不了你。如果你真的打算返回一个可查询的,你不应该编译你的表达式。
你可以做的是:
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,因为它没用,如果需要,应该由调用者使用。