Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate keys with values?

Tags:

c#

linq

I am trying to create a string composed of a key followed by its value, such that the string looks something like:

key;value,key;value,key;value

So far I have tried to use concat:

var originalKeyValues = entity.ChangeTracker.OriginalValues.Keys.Concat(
    entity.ChangeTracker.OriginalValues.Values).ToString();

...but this doesn't seem to produce what I want.

Both Keys and Values are Dictionary<string, object>

like image 409
O.O Avatar asked Jun 13 '11 13:06

O.O


2 Answers

string result=list.Select(w=>w.Key+";"+w.Value).Aggregate((c,n)=>c+","+n);
like image 150
Blindy Avatar answered Sep 24 '22 02:09

Blindy


I would do like this:

var x = new Dictionary<string, string> { { "key", "value" }, { "key2", "value2" } };

Console.WriteLine(
    String.Join(",", x.Select(d => String.Format("{0};{1}", d.Key, d.Value)))
);

From the dictionary select a enumerable of string then join the list by ,.

Output: key;value,key2;value2

like image 26
BrunoLM Avatar answered Sep 27 '22 02:09

BrunoLM