小编Jan*_*ryc的帖子

Linq WHERE EF.Functions.Like - 为什么直接属性有效而反射无效?

我尝试在数据库站点上执行一个简单的 LIKE 操作,同时具有基于通用类型的查询构建服务。然而,我在调试时发现,EF.Functions.Like()使用反射执行并不能按预期工作:

The LINQ expression 'where __Functions_0.Like([c].GetType().GetProperty("FirstName").GetValue([c], null).ToString(), "%Test%")' could not be translated and will be evaluated locally..


与众不同的代码

这有效

var query = _context.Set<Customer>().Where(c => EF.Functions.Like(c.FirstName, "%Test%"));
Run Code Online (Sandbox Code Playgroud)

这会引发警告并尝试在内存中解决

var query = _context.Set<Customer>().Where(c => EF.Functions.Like(c.GetType().GetProperty("FirstName").GetValue(c, null).ToString(), "%Test%"));
Run Code Online (Sandbox Code Playgroud)

Linq 查询生成器或 EF.Functions 不支持反射吗?

抱歉,如果问题看起来很基本,这是我第一次尝试 .NET Core :)

c# linq reflection entity-framework-core .net-core

7
推荐指数
1
解决办法
3600
查看次数

标签 统计

.net-core ×1

c# ×1

entity-framework-core ×1

linq ×1

reflection ×1