c# - 在 C# 中使用 SqlFunctions 修剪字符
问题描述
这是我的代码:
(from customer in db.tblCustomers
join product in db.tblProducts on customer.product_id equals product.id into str
from prod in str.DefaultIfEmpty()
where customer.code.Substring(SqlFunctions.PatIndex("%[^0]%", customer.code).Value - 1) == customerCode
select prod.name).SingleOrDefault();
如您所见,我使用 PatIndex 左修剪字符串,但我也想右修剪零。我怎样才能做到这一点?
例如,如果 customerCode 是“123”,并且在 db 中,如果我有一个值为“001230000”的客户代码,则匹配。
解决方案
验证CustomerCode
全为零后的字符:
(from customer in db.tblCustomers
join product in db.tblProducts on customer.product_id equals product.id into str
from prod in str.DefaultIfEmpty()
let tail = customer.code.Substring(customer.code.IndexOf(customerCode)+customerCode.Length)
where customer.code.Substring(SqlFunctions.PatIndex("%[^0]%", customer.code).Value - 1) == customerCode &&
tail == new String('0', tail.Length)
select prod.name).SingleOrDefault();
推荐阅读
- c# - 为什么这个 azure 函数会产生 JSON 输出?
- c - 声明数组而不指定行大小?
- javascript - 在 Python 请求会话中响应 Javascript 函数
- java - ClassCastException 与 IWorkbenchPage.hideActionSet("org.eclipse.ui.WorkingSetActionSet")
- javascript - 计算对象数组中提供的年份和月份的数组
- sql - 使用 for 循环索引插入表中
- angular - Highchart Stock gui 在 Angular 8 中不起作用
- vue.js - 在 Buefy/Bulma 我如何定义一组用于子元素集的类?
- django - 使用 nginx 和 gunicorn 自定义 json 格式的 django 错误页面
- pandas - 将 pandas DataFrame 拆分为大致相同的块