Answered by:
Automate Comparison csv file and get the difference in to datatable?

Question
-
Hello Friends,
I will get comma separated user profiles file as a feed every day and there will be some updates like few users will be removed, added or modified existing data.
Can one suggest the approach or sample program to get the difference (i need to extract modified rows and newly added rows in to dataset using c#)
Thanks in advance. Please help.
Sunday, March 11, 2012 4:35 AM
Answers
-
I'd use LINQ with this as an example:
class CompareLists { static void Main() { // Create the IEnumerable data sources. string[] names1 = System.IO.File.ReadAllLines(@"../../../names1.txt"); string[] names2 = System.IO.File.ReadAllLines(@"../../../names2.txt"); // Create the query. Note that method syntax must be used here. IEnumerable<string> differenceQuery = names1.Except(names2); // Execute the query. Console.WriteLine("The following lines are in names1.txt but not names2.txt"); foreach (string s in differenceQuery) Console.WriteLine(s); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit"); Console.ReadKey(); } } /* Output: The following lines are in names1.txt but not names2.txt Potra, Cristina Noriega, Fabricio Aw, Kam Foo Toyoshima, Tim Guy, Wey Yuan Garcia, Debra */
JP Cowboy Coders Unite!
- Proposed as answer by Mr. Javaman II Tuesday, March 13, 2012 11:31 PM
- Marked as answer by Mike Dos Zhang Wednesday, March 14, 2012 9:20 AM
Monday, March 12, 2012 3:49 AM -
I think you can import the content form csv file to a DataTable, then compare each row item as your expected. http://windowsclient.net/blogs/faqs/archive/2006/05/30/how-do-i-load-a-csv-file-into-a-datatable.aspx
You can create relationship for the two Tables, and then use the GetChildRows method to do the comparison. http://forums.asp.net/t/1541581.aspx
Or iterator each row and compare. http://stackoverflow.com/questions/164144/c-how-to-compare-two-datatables-a-b-how-to-show-rows-which-are-in-b-but-not
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Mike Dos Zhang Wednesday, March 14, 2012 9:20 AM
Monday, March 12, 2012 1:34 PM
All replies
-
I'd use LINQ with this as an example:
class CompareLists { static void Main() { // Create the IEnumerable data sources. string[] names1 = System.IO.File.ReadAllLines(@"../../../names1.txt"); string[] names2 = System.IO.File.ReadAllLines(@"../../../names2.txt"); // Create the query. Note that method syntax must be used here. IEnumerable<string> differenceQuery = names1.Except(names2); // Execute the query. Console.WriteLine("The following lines are in names1.txt but not names2.txt"); foreach (string s in differenceQuery) Console.WriteLine(s); // Keep the console window open in debug mode. Console.WriteLine("Press any key to exit"); Console.ReadKey(); } } /* Output: The following lines are in names1.txt but not names2.txt Potra, Cristina Noriega, Fabricio Aw, Kam Foo Toyoshima, Tim Guy, Wey Yuan Garcia, Debra */
JP Cowboy Coders Unite!
- Proposed as answer by Mr. Javaman II Tuesday, March 13, 2012 11:31 PM
- Marked as answer by Mike Dos Zhang Wednesday, March 14, 2012 9:20 AM
Monday, March 12, 2012 3:49 AM -
I think you can import the content form csv file to a DataTable, then compare each row item as your expected. http://windowsclient.net/blogs/faqs/archive/2006/05/30/how-do-i-load-a-csv-file-into-a-datatable.aspx
You can create relationship for the two Tables, and then use the GetChildRows method to do the comparison. http://forums.asp.net/t/1541581.aspx
Or iterator each row and compare. http://stackoverflow.com/questions/164144/c-how-to-compare-two-datatables-a-b-how-to-show-rows-which-are-in-b-but-not
Mike Zhang[MSFT]
MSDN Community Support | Feedback to us
- Marked as answer by Mike Dos Zhang Wednesday, March 14, 2012 9:20 AM
Monday, March 12, 2012 1:34 PM -
Hi Javaman II,
Thanks for the response. your approach will work for me. but one I need is first row need to be eliminated when comparing because it will be header and in the result it should have header along with the result after comparison.
Thanks in advance for your help.
- Edited by Tech-Fun Tuesday, March 13, 2012 9:54 PM modied text.
Tuesday, March 13, 2012 9:23 PM -
Hi, I also wrote an article about comparing two data sets using Excel, text editor or database engine. If you would like to check it out, here is the link:
http://efficient-work.blogspot.com/2012/10/compare-two-sets-of-data.html
Feedback is welcomed!Friday, October 19, 2012 3:06 PM