Following on from this post is it possible to create a new list of the duplicate records found (and excluded) in the merge? I want to let the user know which records were excluded. The code I have is working in that it is correctly merging the data and excluding any duplicate keys, but I want to be able to show the keys excluded after the merge.
var fileLocation = @"D:\TFS2010-UK\Merge_Text\PM.INX";
var fileContents =
File.ReadLines(FileLocation, Encoding.Default)
.Select(line => line.Split(','))
.ToDictionary(line => line[0].Replace("\"", ""), line => line[1] + ',' + line[2] + ',' + line[3]);
// define an array of items to be added...
var newContent = new Dictionary<string, string>
{
{ "XYZ789", "\"XYZ789\",1,123.789" },
{ "GHI456", "\"GHI456\",2,123.456" },
{ "ABC123", "\"ABC123\",1,123.123" }
};
var uniqueElements = fileContents.Concat(newContent.Where(kvp => !fileContents.ContainsKey(kvp.Key)))
.OrderBy(x => x.Key)
.ToDictionary(y => y.Key, z => z.Value);
// append new lines to the existing file...
using (var writer = new StreamWriter(fileLocation))
{
// loop through the data to be written...
foreach (var pair in uniqueElements)
{
// and write it to the file...
writer.WriteLine("\"{0}\",{1}", pair.Key, pair.Value);
}
}
Many thanks. Martin