building method for LINQ query to strip chars from records
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.
精彩评论