开发者

building method for LINQ query to strip chars from records

开发者 https://www.devze.com 2022-12-08 07:00 出处:网络
I need to strip the following chars from my records using LINQ to SQL query \"\\\";:\'.,=+!@#$%^&*(_)~{}[]\\\\|<>? aeiouAEIOU\"

I need to strip the following chars from my records using LINQ to SQL query

"\";:'.,=+!@#$%^&*(_)~{}[]\\|<>? aeiouAEIOU"

I can do it with my linq query here

from 开发者_运维百科p in customers 
select new {customer_name = String.Concat(p.First_name.Replace("A","@"),p.Last_name.Replace("A","@")), address =  p.Address.Replace("A","@") }

but I know there must be a better way to do this! I tried a method in c# to strip the chars but since this is linq to sql it doesn't work. The error says "that has no supported translation to SQL"

I need some ideas and code samples on how to write this routine in c#. Or possibly write a sql function to do this and example of how to do that.


However you choose to update the records, you can use the Regex.Replace() method to replace unwanted characters in a string.

For example:

using System.Text.RegularExpressions;

// ...

string unwanted = @"["";:'.,=+!@#$%^&*(_)~{}\[\]\\|<>? aeiouAEIOU]";

string subject = "The quick brown fox jumped over the lazy dog.";

string result = Regex.Replace(subject, unwanted, string.Empty);
// Thqckbrwnfxjmpdvrthlzydg

Read more about Regular Expressions and the .NET implementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号