【发布时间】:2021-11-24 02:02:01
【问题描述】:
我在我的 .NET 代码中有这个扩展方法检查字符串是否是什么东西:
public static bool IsSomething(this string text)
{
var isNothing = string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text);
return !isNothing;
}
但是,我不能在我的 EF Core quereis 中使用它:
dbset.Where(i => i.Title.IsSomething()); // throws non-translatable error
我知道我可以将查询重写为:
dbset.Where(i => i.Title != null && i.Title.Trim() != ""); // this works, but it's too verbose
但我真的不想写那么长的非描述性代码。
我怎样才能让IsSomething 可用于 SQL Server 翻译?
【问题讨论】:
-
旁注:
IsNullOrWhiteSpace为 null 或空字符串返回 true,这就是你所需要的 -
如果你想引入自己的扩展方法,你必须告诉EF如何翻译它们。见this article
标签: c# .net sql-server linq entity-framework-core