ef-core-3.1 - EF Core 3.1 搜索过滤器
问题描述
我正在尝试使用很多选项(字符串输入、选择输入...)创建搜索过滤器。如果 categoryId 大于 0,我需要按类别过滤标签,如果 categoryId 等于 0,则返回所有相关类别的所有标签(或不应用类别选项进行搜索)。但是我的代码在这两种情况下都没有返回任何类别,也没有给我任何错误。我在互联网上找到的很多示例都显示了一个字符串输入是如何工作的。
IQueryable<Tag> tags = _context.Tag
.Where(s => EF.Functions.Like(s.Name, "%" + name + "%"))
.Where(s => EF.Functions.Like(s.Description, "%" + description + "%"))
.Include(s => s.Value)
.Include(s => s.Period);
if (categoryId > 0)
{
tags.Include(s => s.Category).Where(s => s.Category.CategoryId == categoryId);
}
else
{
tags.Include(s => s.Category);
}
谁能解释为什么我的代码不起作用?以及如何实现这个功能?谢谢。
解决方案
正确的代码是:
IQueryable<Tag> tags = _context.Tag
.Where(s => EF.Functions.Like(s.Name, "%" + name + "%"))
.Where(s => EF.Functions.Like(s.Description, "%" + description + "%"))
.Include(s => s.Value)
.Include(s => s.Period);
if (categoryId > 0)
{
tags = tags.Include(s => s.Category).Where(s => s.Category.CategoryId == categoryId);
}
else
{
tags = tags.Include(s => s.Category);
}
推荐阅读
- mysql - 插入几行后,Alter table 命令在 mysql 中不起作用
- laravel-5 - Laravel - 某些路线中的模型策略不起作用
- python - Python 多处理模块意外输出。可能是什么原因造成的?
- javascript - 使用他的类获取指定的父元素ID
- excel - 在我的工作簿中的某些工作表上执行相同的两个计算
- azure - Azure 存储查询因“系统缺少足够的缓冲区空间或队列已满”而失败
- php - 访问这个数据结构 [{"id":"1","name":"2"},{...}]
- ios - iOS:当用作遥控器时,Spotify 如何在后台保持活力?
- git - 如何在客户端 git pull 后运行操作?
- rust - 如何返回对互斥锁内的静态哈希图中的项目的引用?