简体   繁体   中英

Convert Dictionary in List of of strings in C#

I have this code:

var parameters = new Dictionary<string, object>();
parameters.Add("param1", 100);
parameters.Add("param2", "ABC");
parameters.Add("param3", 1.2);

I need to generate the code below with the maximum performance possible, can use Linq, Lambda, etc..!?

var tableName = "table1"
var fields = "param1, param2, param3";
var values = "@param1, @param2, @param3"

var result = String.Format("INSERT INTO {0} ({1}) VALUES ({2})", tableName, fields, values);

... Execute Sql AddParamWithValue....(this is okay I can do it...)

Ok, so, I did a little test and string.join works really well:

param = string.Join(",", parameters.Keys);
value = string.Join(",", parameters.Values);

Here is my test function:

private static void RunLoopTester()
        {
            Console.WriteLine("Building dictionary...");
            var parameters = new Dictionary<string, string>();
            for(var i = 0;i< 10000;i++)
            {
                parameters.Add(string.Format("param{0}",i),string.Format("value{0}", i));
            }
            var stopWatch = new Stopwatch();
            Console.WriteLine("Using foreach statement...");
            stopWatch.Start();
            var param = string.Empty;
            var value = string.Empty;
            foreach(var k in parameters.Keys)
            {
                param += string.Format("{0}{1}", string.IsNullOrEmpty(param) ? string.Empty : ",", k);
                value += string.Format("{0}{1}", string.IsNullOrEmpty(value) ? string.Empty : ",", parameters[k]);

            }
            stopWatch.Stop();
            Console.WriteLine("Elapsed Time: {0}", stopWatch.ElapsedMilliseconds);
            Console.WriteLine("Using simple syntax...");
            param = string.Empty;
            value = string.Empty;
            stopWatch.Reset();
            stopWatch.Start();
            param = string.Join(",", parameters.Keys);
            value = string.Join(",", parameters.Values);
            stopWatch.Stop();
            Console.WriteLine("Elapsed Time: {0}", stopWatch.ElapsedMilliseconds);
            Console.ReadLine();
        }

And the results:

Pick your tester:
1: Timer Test
2: Loop Tester
2
Building dictionary...
Using foreach statement...
Elapsed Time: 1249
Using simple syntax...
Elapsed Time: 1

Edit: Post Accepted. OP mentioned that @param was needed for the proc parameters. This can be done by either put it in the key of the dictionary (much better) or, if you can't, then you can replace:

param = string.Join(",", parameters.Keys);

With:

param = string.Join(",", parameters.Keys).Replace("param","@param");

This increases the time to 2 milliseconds. I tried using a linq select on the keys enumerable, but that increased the time to 4 milliseconds. So the replace is more performant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM