C# LINQ - Extract Values of Dictionary<string,string> as CSV
I'm trying to output the values of the Fields dictionary (Dictionary<string,string>) as a CSV file. I need to put quotes around each value but when I use...
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
I'll spend some time dissecting your code, and like all of your previous code suggestions, I will learn a lot in the process. Thanks again!!
Fernando Soto
Hi nightshadz;
The Aggregate method makes it simple to perform a calculation / manipulation over a sequence of values. This method works by calling function in the lambda expression one time for each element in the source sequence. Each time the function is called, the Aggregate method supply both the element from the sequence and an aggregated value (as the first argument to function). The first element of the source is used as the initial aggregate value. The result of function replaces the previous aggregated value. The Aggregate method returns the final result of the function.
From the above inside of the Aggregate method we have the empty string, "", this is here for two reasons, first to tell the method of the return type is a string and not a KeyValuePaire and secondly as the initial value for the aggregateFields. Next we look at lambda expression, (aggregateFields, nextField), where aggregateFields is the accumulated values of all the iterations and passed to the function with each iteration and the nextField is the next object from the Dictionary, KeyValuePaire also passed to the function. Now you have the function itself which is applied to each element of the Dictionary. Because we are concatenating strings as follows. The accumulated string concatinated with a " concatenated to the Value part of the KeyValuePair concatenated to the final " and a comma. When the Aggregate method is done the last method TrimEnd removes the final comma.
btw, you only need to enclose values in a CSV, when it contains a quotation mark or the column terminator.