使用 ac# lambda 将 NameValueCollection 转换为查询字符串是否高效?
在研究如何将 NameValueCollection 转换为查询字符串时,我遇到了不同的方法。我很好奇较短的 lambda 语法是否尽可能高效。
如何使用迭代函数将 NameValueCollection 转换为(查询)字符串。
public static String ConstructQueryString(NameValueCollection parameters)
{
List<String> items = new List<String>();
foreach (String name in parameters)
items.Add(String.Concat(name, "=", System.Web.HttpUtility.UrlEncode(parameters[name])));
return String.Join("&", items.ToArray());
}
将 NameValueCollection 加入 C# 中的查询字符串 使用 lambda 表达式,该表达式看起来不错,但我不确定它是否是有效的代码。
private static string JoinNvcToQs(NameValueCollection qs)
{
return string.Join("&", Array.ConvertAll(qs.AllKeys, key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(qs[key]))));
}
In researching how to convert a NameValueCollection to a querystring, I have come across different methods. I am curious if the shorter lambda syntax is as efficient as it could be.
How to convert NameValueCollection to a (Query) String using a iterating function.
public static String ConstructQueryString(NameValueCollection parameters)
{
List<String> items = new List<String>();
foreach (String name in parameters)
items.Add(String.Concat(name, "=", System.Web.HttpUtility.UrlEncode(parameters[name])));
return String.Join("&", items.ToArray());
}
Join a NameValueCollection into a querystring in C# uses a lambda expression, which looks nice but I'm not sure if it is efficient code.
private static string JoinNvcToQs(NameValueCollection qs)
{
return string.Join("&", Array.ConvertAll(qs.AllKeys, key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(qs[key]))));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会这样做:
这样你就可以创建更少的对象(必须由垃圾收集器清理)
I would do it like this:
This way you create less objects (that have to be cleaned up by the garbage collector)
首先,您能做的最好的事情就是测试并查看性能是否适合您的应用程序,我们可以告诉您有关性能的一般性信息,但最终取决于您的需求,只有您知道答案。
至于手头的问题,每当您使用委托(这是 lambda 创建的)而不是直接执行代码时,性能都会受到影响。在大多数情况下,命中是可以接受的,但如果此代码需要绝对最佳的性能(假设它位于内部循环中),那么您需要使用第一种方法。
也就是说,如果您正在创建查询字符串,那么您可能即将访问数据库,这可能比首先创建查询字符串的任何一种方法都要花费更长的时间。
First of all, the best thing you can do is test and see if the performance is acceptable for your application, we can tell you generalities about performance but in the end it comes down to your needs and only you know the answers to that.
As to the question at hand, any time you use a delegate (which is what a lambda creates) rather than executing the code directly you'll take a performance hit. In most cases the hit is acceptable but if this code needs the absolute best possible performance (say it's in an inner loop) then you need to go with your first method.
That said, if you're creating a querystring, presumably you're about to hit the database which will likely take considerably longer than either method of creating the querystring in the first place.
NameValueCollection 的
ToString
方法将为您构建查询字符串。我还没有做过任何基准测试,但我想这个实现会比使用 lambda 或 foreach 更有效。(
ToString
解决方案似乎没有详细记录;我之所以找到它是因为这个答案 在代码示例中使用了它。)NameValueCollection's
ToString
method will build the query string for you. I haven't done any benchmarking, but I'd imagine the implementation would be more efficient than something using lambdas or foreach.(The
ToString
solution doesn't seem to be well-documented; I only found it because this answer used it in a code sample.)