Skip to content

Instantly share code, notes, and snippets.

@ddpruitt
Created July 31, 2023 18:25
Show Gist options
  • Save ddpruitt/951ec027c0d56b9a6fa3e7dfd51829ca to your computer and use it in GitHub Desktop.
Save ddpruitt/951ec027c0d56b9a6fa3e7dfd51829ca to your computer and use it in GitHub Desktop.
Comparing Dictionaries using LINQPad
var d1 = new Dictionary<int, string>
{
{0, "0" },
{1, "0" },
{2, "0" },
{3, "3" },
{4, "0" },
{5, "0" },
{10, "0" },
{11, "0" },
{12, "12" },
{13, "0" },
{14, "0" }
};
d1.Dump(nameof(d1));
var d2 = new Dictionary<int, string>
{
{0, "0" },
{1, "0" },
{2, "2" },
{3, "0" },
{4, "0" },
{5, "0" },
{6, "0" },
{7, "7" },
{8, "0" },
{9, "0" }
};
d2.Dump(nameof(d2));
((d1.Count == d2.Count) && !d1.Except(d2).Any()).Dump("Does d1 Equal d2?");
((d1.Count == d1.Count) && !d1.Except(d1).Any()).Dump("Does d1 Equal d1?");
d1.Except(d2).Dump("d1 where it is different from d2");
d2.Except(d1).Dump("d2 where it is different from d1");
var filterKeys = new []{0,2,9,13};
filterKeys.Dump("Filter keys");
d1.Where(d => filterKeys.Contains(d.Key)).Dump("d1 but only on the Filtered Keys");
d2.Where(d => filterKeys.Contains(d.Key)).Dump("d2 but only on the Filtered Keys");
d1.Where(d => filterKeys.Contains(d.Key)).Except(d2).Dump("d1 where it is different from d2 but only on the Filtered Keys");
d2.Where(d => filterKeys.Contains(d.Key)).Except(d1).Dump("d2 where it is different from d1 but only on the Filtered Keys");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment