用于LINQ查询的构建方法,用于从记录中去除字符

时间:2009-10-14 15:56:51

标签: c# .net linq linq-to-sql

我需要使用LINQ to SQL查询从记录中删除以下字符

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

我可以在这里使用我的linq查询

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","@") }

但我知道必须有更好的方法来做到这一点!我在c#中尝试了一个方法去除字符,但因为这是linq到sql它不起作用。错误显示“没有支持的SQL转换”

我需要一些关于如何在c#中编写此例程的想法和代码示例。或者可能编写一个sql函数来执行此操作以及如何执行此操作的示例。

1 个答案:

答案 0 :(得分:1)

但是,如果您选择更新记录,则可以使用Regex.Replace()方法替换字符串中不需要的字符。

例如:

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.NET implementation