开发者

C# Multi Replace Idea

开发者 https://www.devze.com 2023-02-02 22:45 出处:网络
I know the want for a \"multi replace\" in C# is no new concept, there are tons of solutions out there. However, I haven\'t came across any elegant solution that jives well with Linq to Sql. My propos

I know the want for a "multi replace" in C# is no new concept, there are tons of solutions out there. However, I haven't came across any elegant solution that jives well with Linq to Sql. My proposal is to create an extension method (called, you guessed it, MultiReplace) that returns a lambda expression whi开发者_JAVA百科ch chains multiple calls of Replace together. So in effect you would have the following:

x => x.SomeMember.MultiReplace("ABC", "-")

// Which would return a compiled expression equivalent to:

x => x.SomeMember.Replace("A", "-").Replace("B", "-").Replace("C", "-")

I'd like your thoughts/input/suggestions on this. Even if it turns out to be a bad idea, it still seems like a wicked opportunity to dive into expression trees. Your feedback is most appreciated.

-Eric


I'm not completely sure why you would want to do what you're describing. However if your motivation is to make more readable linq statements, by condensing some filtering logic, I suggest to look into the Specification Pattern.

If you only want to transform the result however, I would suggest to just do it in code, as there would only be a marginal benefit transforming on the server.

Some more examples on the Specification Pattern and Linq-to-SQL


I'm not sure what MultiReplace should be doing, or why you want to mix it with Linq to Sql. (Anything truly working with Linq to Sql would be translatable into SQL, which would be quite a lot of work, I think.)

The best solution I can think of is Regular Expressions. Why not use them? Linq to Sql may even translate them for you already, since MS SQL supports regular expressions.

x => Regex.Replace(x, "A|B|C", "-")


To me, this seems like a REALLY bad idea, it could be just because of your example, but the syntax you have listed to me is very confusing.

Reading this I would expect that

x => x.SomeMember.MultiReplace("ABC", "-")

If using the following text

ABC This Is A Test ABC Application

You would get something like

--- This Is A Test --- Application

But you are actually saying that it would be

--- This Is - Test --- -pplication

Which I see as problematic....

I'd also mention here that I don't really see a n urgent need for something like this. I guess if there was a need to do multiple replacemnts I'd do one of the following.

  1. Chain them myself "myInput".Replace("m", "-").Replace("t", "-")
  2. Create a function/method that accepts an ARRAY or List of strings for the matches, then a replacement character. Keeping it easy to understand.


Maybe there's no method that will do this, but you could simply nest the calls. Or make a static method that takes in a string and an array containing all its replacements. Another problem is that you need to actually specify a pair (the original substring, and its replacement).

I've never needed/wanted a feature like this.


The best way to do this would be:

static string MultiReplace(string this CSV, string Orig, string Replacement)
{
    string final = "";
    foreach (string s in CSV.Split(','))
    {
        final += s.Replace(Orig, Replacement);
    }
    return final;
}

Then you could call it with no ambiguity:

x => x.SomeMember.MultiReplace("A,B,C", "-")

If you expect that these strings will be longer than 3-4 values in the CSV, you might want to drop the concatenation in favor of a StringBuilder.

0

精彩评论

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

关注公众号