Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
started c# last time, but now i got a Problem...i Need a snippet 2 understand how to to it.

Problem:

private void button1_Click(object sender, EventArgs e)
        {
            using (StreamWriter writer = new StreamWriter(@"C:/WEBUPLOAD/UPLOAD/Output.csv", true))
            {
                using (StreamReader reader = new StreamReader(@"C:/input.csv", System.Text.Encoding.Default))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] items = line.Replace("\"", "").Trim().Split(';');
                        if (items.Length != 4) continue;
                        int i1, i2;
                        bool b1 = int.TryParse(items[2], out i1);
                        bool b2 = int.TryParse(items[3], out i2);           
                        items[2] = Math.Max(0, i1 -i2).ToString();
                        line = String.Join("~", items, 0, 3);
                        writer.WriteLine(line);
                    }



I wanna rewrite the Code above, but dont now how to do that with two files.



I get a 1 File that looks like that:

"";"";0
" 09000";"Catalog 1";10148
" 09001";"Catalog 2";20
" 09002";"Catalog 3";166

I get a 2 File that looks like that:

" 09000";476
" 09003";10
" 09005";5

What to do?

the Output file must look like that:

09000~Catalog 1~9672
09001~Catalog 2~10
09002~Catalog 3~161

I need to substract Field 3 from File1, with Field 2 from File 2 and get a Output File like above.
Posted
Updated 7-Apr-13 23:47pm
v6
Comments
Prasad Khandekar 8-Apr-13 5:57am    
Basically you will require to find a matching row from second file. I am assuming that this will be done using first column. I will suggest that you import these files in an in-memory database like SQLite and then use a JOIN query to fetch the data. From the output of this qeury you can easily construct the desired output. Have a look at (http://www.filehelpers.com/).

If the files you are dealing with are small say 500-1K records then you can load data from one file in a HashTable (http://msdn.microsoft.com/en-us/library/system.collections.hashtable(v=vs.110).aspx) and while reading the second file line by line find the matching key in HashTable, output into a new file if you find a matching key.

I would use a database for that. If you don't connect already to a server, just use some of the embedded editions (there are several from SQL CE to SQLite and so on...). If you are using some MSSQL edition, create a session with two temporary tables.
Now, use your code, or some other code to read the CSV and insert the data in the two tables. Make a proper join and select the result. Than delete the data. That's it.

You could use lists and linq to manage all this in memory, but it depends on the amount of the data you have to process.

If you have lot of data, and an sql server that is remote, you can still use the embedded approach for this task.
 
Share this answer
 
Comments
Zoltán Zörgő 8-Apr-13 6:51am    
[OP:]
nah i wanna do it like above..without sql, cant do that..

[Me:]
What do you mean exactly by "cant do that"? Why "can't you"?
As I mentioned, you could do this in memory too with or without linq, but it depends on the amount of data you have to parse. How many records do you have to process?
nah i wanna do it like above..without sql, cant do that..
 
Share this answer
 
Comments
Zoltán Zörgő 8-Apr-13 6:51am    
This is not an answer. Please use the page widgets! If you have a comment or question to my answer post it there, not as answer since it is not.
By the way what do you mean exactly by "cant do that"?

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