Обрезка символов с помощью SqlFunctions в C # - PullRequest
1 голос
/ 01 апреля 2019

Это мой код:

(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», а в дБ, если у меня есть код клиента со значением «001230000», это совпадение.

Ответы [ 2 ]

0 голосов
/ 01 апреля 2019

Проверьте символы после 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();
0 голосов
/ 01 апреля 2019

Вместо использования SqlFunctions.PatIndex. Вы можете использовать метод строки 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();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...