I have two dataTables as follows
1) dtExistingZipCodeInDB (data from database)
2) dtCSVExcelSource (data from csv source which is to be processed)
I have two requirements
1) List all the retired zip codes (zip codes that are present in dtExistingZipCodeInDB but not in dtCSVExcelSource)
2) UnChanged zip codes (zip codes that are present both in dtExistingZipCodeInDB and dtCSVExcelSource)
I can use the Merge to get the retired zip codes. How do I get the unchanged zip codes?
Framework: .Net 3.0
//Note: dtExistingZipCodeInDB and dtCSVExcelSource has the same columns
dtCSVExcelSource.Merge(dtExistingZipCodeInDB);
DataTable dtRetiredZipDataTable = dtCSVExcelSource.GetChanges();
string retiredZipCodes = GetStringFromDataTable(dtRetiredZipDataTable, "ZipCode");
Thanks
Lijo
With the .NET 3.0 requirement, the Intersect LINQ extension method is not available, but you can provide your own extension method.
All you need is the
MatchingRowsextension method (see below in the demo code) and then do:Then you can loop over unchangedZipCodes, which will contain only those rows with ZipCodes in common between dtExistingZipCodeInDB and dtCSVExcelSource.
Below is demo code I wrote using LINQPad. I love LINQPad — it’s great for proof of concept or scratchpadding/sandboxing some code quickly. But it is not required for the solution to this question.
Outputs: