C# 多替换想法
我知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不完全确定你为什么想做你所描述的事情。但是,如果您的动机是通过压缩一些过滤逻辑来制作更具可读性的 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
我不确定 MultiReplace 应该做什么,或者为什么要将它与 Linq to Sql 混合。 (任何真正使用 Linq to Sql 的东西都可以翻译成 SQL,我认为这将是相当大量的工作。)
我能想到的最好的解决方案是正则表达式。为什么不使用它们呢? Linq to Sql 甚至可以为您翻译它们,因为 MS 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.
对我来说,这似乎是一个真的坏主意,可能只是因为你的例子,但你向我列出的语法非常令人困惑。
读这篇文章我希望
如果使用以下文本
你会得到类似的东西
但你实际上是在说这将是
我认为有问题的......
我还在这里提到我真的没有看到紧迫的需要对于这样的事情。我想如果需要进行多次替换,我会执行以下操作之一。
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
If using the following text
You would get something like
But you are actually saying that it would be
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.
也许没有方法可以做到这一点,但您可以简单地嵌套调用。或者创建一个静态方法,它接受一个字符串和一个包含其所有替换的数组。另一个问题是您需要实际指定一对(原始子字符串及其替换)。
我从来没有需要/想要这样的功能。
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.
执行此操作的最佳方法是:
然后您可以毫无歧义地调用它:
如果您预计这些字符串将比 CSV 中的 3-4 个值长,您可能希望放弃连接而改用 StringBuilder。
The best way to do this would be:
Then you could call it with no ambiguity:
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.