2009-10-14 52 views
0

我需要剥去使用LINQ我的记录下面的字符到SQL查询字符的建筑方法对LINQ查询剥去记录

"\";:'.,[email protected]#$%^&*(_)~{}[]\\|<>? 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

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

例如:

using System.Text.RegularExpressions; 

// ... 

string unwanted = @"["";:'.,[email protected]#$%^&*(_)~{}\[\]\\|<>? 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

+0

linq错误说“不支持转换为SQL” - 任何其他想法? – kiev 2009-10-14 18:15:14

+1

获取客户对象,更新它们,然后将更新的值保存回数据库,而不是尝试将它们更新为选择的一部分? – 2009-10-14 21:18:22