-2

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

Community
  • 1
  • 1
MartinS
  • 111
  • 1
  • 14
  • 1
    You have to show the code that didn't work and mention whats wrong with it. Then we can help to fix it. Otherwise this question is just asking us to do your work. – Tim Schmelter Dec 15 '15 at 10:38
  • Apologies. Hopefully I have added sufficient detail to make the question clearer. – MartinS Dec 15 '15 at 13:56

1 Answers1

0
var removedKeys = newContent.Where(kvp => fileContents.ContainsKey(kvp.Key))
    .Select(kvp => kvp.Key);

Simply select the keys in newContent that are already contained in fileContents.

TVOHM
  • 2,740
  • 1
  • 19
  • 29