Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am going to parse first two csv lines (headers,values) and then split them on base of comma ',' and then add them both parallel to the collection. I am able to do this with dictionary but when the two headers same or empty it gives error "n item with the same key has already been added in dictionary". In my case Tkey=First Line Header Values and TValue = Second Line Values.

What I have tried:

I have tried C# Generics Dictionary and HashTable feature but both have limitations that Key must be unique.
Posted
Updated 30-Apr-18 16:07pm
v2

You seem to have an issue with the fact that you have "empty lines" and "duplicate headers".

Nobody can figure out if you think this is a "feature" or a "problem".

You have gotten reasonable answers to a foggy "question" (if you can call it that).

You need a better "problem definition".
 
Share this answer
 
Comments
Waqar (Vicky) 2-May-18 15:48pm    
i am able to solve my problem with
List<KeyValuePair<string, string>>
it was my first time to put my question on this platform for help
well learn too many things/
Next time i will make my question more clear
Thanks
Patrice T 2-May-18 15:58pm    
The clearer the question, the clearer the solutions.
Waqar (Vicky) 3-May-18 6:17am    
Okay Got it :-)
I have no idea what you're doing but it sounds like a List<KeyValuePair<string, string>> could do the trick.
 
Share this answer
 
v2
Comments
Waqar (Vicky) 29-Apr-18 16:26pm    
Thanks for you reply @Dave Kreskowiak

actually i am doing here to read first two lines of csv
and then add them both in my data table columns collection.

I have to add both column names like (Name,Father Name,Age) and Column values Like(Adam,Yann,32) both in the collection.
here is my code given below.
.
var data = new Dictionary<string, string>();
var columnHeaders = headerLine.Split(',');
var dataValues = dataLine.Split(',');

for (var i = 0; i < columnHeaders.Count(); i++)
{
data.Add(columnHeaders[i], dataValues[i]);
}
foreach (var pair in data)
{
CurrentRecord.MappingResourceValues.Add(new DataModels.Tables.MappingResourceValue(0, null, pair.Key, pair.Value, 0, CurrentRecord.Id));
}
Dave Kreskowiak 29-Apr-18 19:36pm    
I still don't know what you're really doing with data or what you're even trying to accomplish with all this. You're code doesn't make much sense and doesn't really show anything useful as far as storing the data for any practical purpose.

Richard Deeming 30-Apr-18 13:07pm    
Did you mean List<KeyValuePair<string, string>>? .NET doesn't have a List class with two generic parameters...
Dave Kreskowiak 30-Apr-18 15:52pm    
Whoops. Yes, I did.
Waqar (Vicky) 2-May-18 15:45pm    
Yup exactly you got my pint i was talking about this.
Thanks.
Quote:
I have tried C# Generics Dictionary and HashTable feature but both have limitations that Key must be unique.

Non empty unique keys are the principle of dictionary. No dictionary will break this rule, ever.
The dictionary smells like a wrong solution to your problem.

In order to get a solution, you need to explain exactly what you want to do with a sample input and output.
 
Share this answer
 
Comments
Waqar (Vicky) 2-May-18 15:48pm    
i am able to solve my problem with
List<KeyValuePair<string, string>>
it was my first time to put my question on this platform for help
well learn too many things/
Next time i will make my question more clear
Thanks
How about using two Dictionary<int,string> -- one for column names and one for data?

However, I recommend reading them into a DataTable (you never know what the future may hold) -- name the columns with the index and set the Captions to the name.
 
Share this answer
 
Comments
Waqar (Vicky) 29-Apr-18 16:22pm    
Thanks for you reply @PIEBALDconsult
If i use Disctionary<int,string>
then i have to add both column names like (Name,Father Name,Age) and Column values Like(Adam,Yann,32) both in the collection.
here is my code given below.
.
var data = new Dictionary<string, string>();
var columnHeaders = headerLine.Split(',');
var dataValues = dataLine.Split(',');

for (var i = 0; i < columnHeaders.Count(); i++)
{
data.Add(columnHeaders[i], dataValues[i]);
}
foreach (var pair in data)
{
CurrentRecord.MappingResourceValues.Add(new DataModels.Tables.MappingResourceValue(0, null, pair.Key, pair.Value, 0, CurrentRecord.Id));
}
Waqar (Vicky) 2-May-18 15:48pm    
i am able to solve my problem with
List<KeyValuePair<string, string>>
it was my first time to put my question on this platform for help
well learn too many things/
Next time i will make my question more clear
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900