C# 多替换想法

发布于 2024-10-10 09:22:37 字数 462 浏览 2 评论 0原文

我知道 C# 中“多重替换”的需求并不是什么新概念,有大量的解决方案。然而,我还没有遇到任何与 Linq to Sql 配合良好的优雅解决方案。我的建议是创建一个扩展方法(您猜对了,称为 MultiReplace),该方法返回一个 lambda 表达式,该表达式将多个 Replace 调用链接在一起。因此,实际上您将得到以下内容:

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

// Which would return a compiled expression equivalent to:

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

我想了解您对此的想法/意见/建议。即使这被证明是一个坏主意,它似乎仍然是一个深入表达树的邪恶机会。非常感谢您的反馈。

-埃里克

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 which 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

喜爱纠缠 2024-10-17 09:22:37

我不完全确定你为什么想做你所描述的事情。但是,如果您的动机是通过压缩一些过滤逻辑来制作更具可读性的 linq 语句,我建议您查看 规范模式

但是,如果您只想转换结果,我建议只在代码中进行,因为在服务器上进行转换只会带来边际效益。

关于规范模式和 Linq-to-SQL 的更多示例

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

若言繁花未落 2024-10-17 09:22:37

我不确定 MultiReplace 应该做什么,或者为什么要将它与 Linq to Sql 混合。 (任何真正使用 Linq to Sql 的东西都可以翻译成 SQL,我认为这将是相当大量的工作。)

我能想到的最好的解决方案是正则表达式。为什么不使用它们呢? Linq to Sql 甚至可以为您翻译它们,因为 MS SQL 支持正则表达式。

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

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", "-")
千笙结 2024-10-17 09:22:37

对我来说,这似乎是一个真的坏主意,可能只是因为你的例子,但你向我列出的语法非常令人困惑。

读这篇文章我希望

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

如果使用以下文本

ABC This Is A Test ABC Application

你会得到类似的东西

--- This Is A Test --- Application

但你实际上是在说这将是

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

我认为有问题的......

我还在这里提到我真的没有看到紧迫的需要对于这样的事情。我想如果需要进行多次替换,我会执行以下操作之一。

  1. 我自己将它们链接起来 "myInput".Replace("m", "-").Replace("t", "-")
  2. 创建一个函数/方法,它接受匹配的 ARRAY 或字符串列表,然后是替换字符。保持易于理解。

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.
如梦 2024-10-17 09:22:37

也许没有方法可以做到这一点,但您可以简单地嵌套调用。或者创建一个静态方法,它接受一个字符串和一个包含其所有替换的数组。另一个问题是您需要实际指定一对(原始子字符串及其替换)。

我从来没有需要/想要这样的功能。

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.

痴情换悲伤 2024-10-17 09:22:37

执行此操作的最佳方法是:

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;
}

然后您可以毫无歧义地调用它:

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

如果您预计这些字符串将比 CSV 中的 3-4 个值长,您可能希望放弃连接而改用 StringBuilder。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文