asp.net-core - .NET Core - EF - 尝试用数字匹配/替换字符串,导致 System.InvalidOperationException
问题描述
我必须在 SQL 中以最小/最大范围匹配邮政编码的记录
挑战在于数据质量很差,一些邮政编码不仅仅是数字
所以我尝试通过丢弃坏的邮政编码甚至只保留数字来匹配“好的邮政编码”
- 我不知道如何使用 Regex.Replace(..., @"[^\d]", "") 而不是 Regex.Match(..., @"\d") 来适应下面的查询
- 我在运行时收到以下代码错误
我试过了
- 正则表达式.IsMatch
- SqlFunctions.IsNumeric
它们都在运行时导致错误,这里是代码:
var data = context.Leads.AsQueryable();
data = data.Include(p => p.Company).Include(p => p.Contact);
data = data.Where(p => Regex.IsMatch(p.Company.ZipCode, @"\d"));
data = data.Where(p => Convert.ToInt32(p.Company.ZipCode) >= range.Min);
data = data.Where(p => Convert.ToInt32(p.Company.ZipCode) <= range.Max);
这是错误:
System.InvalidOperationException: The LINQ expression 'DbSet<Lead>
.Join(
outer: DbSet<Company>,
inner: l => EF.Property<Nullable<int>>(l, "CompanyId"),
outerKeySelector: c => EF.Property<Nullable<int>>(c, "Id"),
innerKeySelector: (o, i) => new TransparentIdentifier<Lead, Company>(
Outer = o,
Inner = i
))
.Where(l => !(Regex.IsMatch(
input: l.Inner.ZipCode,
pattern: "\d")))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
我不知道如何解决这个问题。我真的不明白 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 在这里有何帮助
我做错了什么?
感谢您的帮助
解决方案
我尝试了所有可以想象的
各种 linq/ef 技巧,我什至尝试定义一个从未找到的 DBFunction
一旦我有一个直接用 SQL 编写的正在运行的存储过程,我最终得到了一个列表,而不是 IQueryable,所以我回到了#1
最后,我刚刚在我的表中创建了一个新字段:
邮编号码
它包含一个经过过滤的、转换后的邮政编码字符串
推荐阅读
- c# - 从另一个查询中存在的查询中过滤值
- javascript - Ajax 成功事件中显示 Laravel 验证错误
- c++ - 简单的多播应用程序无法在同一网络上的不同计算机上运行
- javascript - JavaScript 中的数组是用什么数据结构实现的?
- c# - EF Core - 为多个关系重用相同的连接表实体
- unity3d - 使用 IL2CPP 构建错误:“System.ComponentModel.Win32Exception:指定的可执行文件不是此 OS 平台的有效应用程序。”
- r - 按指定列和指定值过滤数据框
- python - 我在哪里可以找到 matplotlib 中的所有自定义属性?
- angularjs - 在 $http Service AngularJS 1.5.5 中设置超时
- authentication - 在 Google Chrome 中传递基本身份验证凭据再次显示弹出窗口