【问题标题】:Trimming characters with SqlFunctions in C#在 C# 中使用 SqlFunctions 修剪字符
【发布时间】:2019-08-22 14:58:58
【问题描述】:

这是我的代码:

(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”的客户代码,则匹配。

【问题讨论】:

  • 反转字符串,然后是相同的代码。

标签: c# entity-framework linq tsql


【解决方案1】:

验证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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多