这是我的代码:
(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'的客户代码,则表示匹配。
答案 0 :(得分:0)
验证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();
答案 1 :(得分:-1)
代替使用SqlFunctions.PatIndex。您可以使用string的TrimStart()和TrimEnd()方法。
例如
(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.Contains(customerCode.TrimStart(new Char[] { '0' }).TrimEnd(new Char[] { '0' }))
select prod.name).SingleOrDefault();