代码之家  ›  专栏  ›  技术社区  ›  petko_stankoski

用C语言中的SqlFunctions修剪字符#

  •  0
  • petko_stankoski  · 技术社区  · 6 年前

    这是我的代码:

    (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左修剪字符串,但我也希望右修剪零。我该怎么做?

    例如,如果customer code为“123”,并且在db中,如果我有一个值为“001230000”的客户代码,则它是匹配的。

    0 回复  |  直到 6 年前
        1
  •  0
  •   NetMage    6 年前

    在之后验证字符 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();