How to compare IEnumerable<Dictionary> based on matching keys in C#How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How can I detect if this dictionary key exists in C#?How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?C# WPF - Listview Binding not workingHow do I turn a C# object into a JSON string in .NET?How to create a list from two values?Querying xml child elements with prefixed namespace using LINQ to XMLHow to export a field that contain a comma into a CSV file in C sharp codeit is impossible to exchange a member, determining certificate of an object. Try to add a new object with a new certificateReading csv values into Dictionary with Where clause

How can ping know if my host is down

Microchip documentation does not label CAN buss pins on micro controller pinout diagram

Is there a way to have vectors outlined in a Vector Plot?

How does electrical safety system work on ISS?

What kind of floor tile is this?

Were Persian-Median kings illiterate?

Has the laser at Magurele, Romania reached a tenth of the Sun's power?

Which Article Helped Get Rid of Technobabble in RPGs?

How to explain what's wrong with this application of the chain rule?

When were female captains banned from Starfleet?

PTIJ: Why is Haman obsessed with Bose?

How much theory knowledge is actually used while playing?

A Trivial Diagnosis

C++ check if statement can be evaluated constexpr

How do I fix the group tension caused by my character stealing and possibly killing without provocation?

What is the highest possible scrabble score for placing a single tile

Can you use Vicious Mockery to win an argument or gain favours?

How many arrows is an archer expected to fire by the end of the Tyranny of Dragons pair of adventures?

Pre-mixing cryogenic fuels and using only one fuel tank

I found an audio circuit and I built it just fine, but I find it a bit too quiet. How do I amplify the output so that it is a bit louder?

Shouldn’t conservatives embrace universal basic income?

Why do ¬, ∀ and ∃ have the same precedence?

Taxes on Dividends in a Roth IRA

Can I say "fingers" when referring to toes?



How to compare IEnumerable> based on matching keys in C#


How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How can I detect if this dictionary key exists in C#?How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?C# WPF - Listview Binding not workingHow do I turn a C# object into a JSON string in .NET?How to create a list from two values?Querying xml child elements with prefixed namespace using LINQ to XMLHow to export a field that contain a comma into a CSV file in C sharp codeit is impossible to exchange a member, determining certificate of an object. Try to add a new object with a new certificateReading csv values into Dictionary with Where clause













0















I want to compare data from two different lists, a source list & a target list. They are two different IEnumerable<Dictionary<string, object>>



I am running two tests and the result for each test should output the missing records from the target and assign the missing records to a new IEnumerable<Dictionary<string, object>>.



The keys in both Dictionary<string, object> are always the same. However, the length of the list and the values in the dictionary might not be.



List 1:



enter image description hereenter image description hereenter image description here



List 2:



enter image description hereenter image description hereenter image description here



Here is how I tried but it only compares the first key and value of the lists.
I want to know if there are any different values with the same keys in each list.



My Controller:



 [HttpPost]
public ActionResult TestResult(ValidationTestVM model)


model.NotFoundInSource = ServiceLayer.RecordCountResults(sourceQueryResults, targetQueryResults).ToList();

model.NotFoundInTarget = ServiceLayer.RecordCountResults(targetQueryResults, sourceQueryResults).ToList();



service class:



 public static IEnumerable<Dictionary<string, object>> RecordCountResults(IEnumerable<Dictionary<string, object>> source, IEnumerable<Dictionary<string, object>> target)



This does not seem to work correctly. Can someone tell what am I doing wrong?



A dummy method that creates the sample data and showing how I expect to see the returns lists:



 public static void DummyData(IEnumerable<Dictionary<string, object>> source, IEnumerable<Dictionary<string, object>> target)

// Dummy Data
List<Dictionary<string, object>> lSource = source.ToList();
List<Dictionary<string, object>> lTarget = target.ToList();

for (int i = 0; i < 3; i++)

var sourceDic1 = new Dictionary<string, object>();
sourceDic1.Add("Field1", "2017");
sourceDic1.Add("Field2", "2018");
sourceDic1.Add("Field3", "2019");
sourceDic1.Add("Field4", "2018_E_" + i);

lSource.Add(sourceDic1);



for (int i = 2; i < 4; i++)

var targetDic2 = new Dictionary<string, object>();
targetDic2.Add("Field1", "2017");
targetDic2.Add("Field2", "2018");
targetDic2.Add("Field3", "2019");
targetDic2.Add("Field4", "2018_E_" + i);

lTarget.Add(targetDic2);



// Results
var DoesNotExistInTarget = new List<Dictionary<string, object>>();
var DoesNotExistInSource = new List<Dictionary<string, object>>();

for (int i = 0; i < 2; i++)

var MissingDic1 = new Dictionary<string, object>();
MissingDic1.Add("Field1", "2017");
MissingDic1.Add("Field2", "2018");
MissingDic1.Add("Field3", "2019");
MissingDic1.Add("Field4", "2018_E_" + i);

DoesNotExistInTarget.Add(MissingDic1);


for (int i = 3; i < 4; i++)

var MissingDic2 = new Dictionary<string, object>();
MissingDic2.Add("Field1", "2017");
MissingDic2.Add("Field2", "2018");
MissingDic2.Add("Field3", "2019");
MissingDic2.Add("Field4", "2018_E_" + i);

DoesNotExistInSource.Add(MissingDic2);











share|improve this question
























  • What do you want this to yield? The differences in the sets?

    – Jlalonde
    Mar 8 at 0:45











  • So you want to find the difference based on the values of dictioary? And the keys will always be there and same (3 keys as shown). Also do you want to find the difference of 1st item in list 1 with 1st item in list 2 and so on or with all the dictionary keys in list 1 with all dictionary keys in list 2? Also will dictionary keys in one list (keys in 1st item in list 1 and 2nd item in list 1) be different?

    – peeyush singh
    Mar 10 at 0:37















0















I want to compare data from two different lists, a source list & a target list. They are two different IEnumerable<Dictionary<string, object>>



I am running two tests and the result for each test should output the missing records from the target and assign the missing records to a new IEnumerable<Dictionary<string, object>>.



The keys in both Dictionary<string, object> are always the same. However, the length of the list and the values in the dictionary might not be.



List 1:



enter image description hereenter image description hereenter image description here



List 2:



enter image description hereenter image description hereenter image description here



Here is how I tried but it only compares the first key and value of the lists.
I want to know if there are any different values with the same keys in each list.



My Controller:



 [HttpPost]
public ActionResult TestResult(ValidationTestVM model)


model.NotFoundInSource = ServiceLayer.RecordCountResults(sourceQueryResults, targetQueryResults).ToList();

model.NotFoundInTarget = ServiceLayer.RecordCountResults(targetQueryResults, sourceQueryResults).ToList();



service class:



 public static IEnumerable<Dictionary<string, object>> RecordCountResults(IEnumerable<Dictionary<string, object>> source, IEnumerable<Dictionary<string, object>> target)



This does not seem to work correctly. Can someone tell what am I doing wrong?



A dummy method that creates the sample data and showing how I expect to see the returns lists:



 public static void DummyData(IEnumerable<Dictionary<string, object>> source, IEnumerable<Dictionary<string, object>> target)

// Dummy Data
List<Dictionary<string, object>> lSource = source.ToList();
List<Dictionary<string, object>> lTarget = target.ToList();

for (int i = 0; i < 3; i++)

var sourceDic1 = new Dictionary<string, object>();
sourceDic1.Add("Field1", "2017");
sourceDic1.Add("Field2", "2018");
sourceDic1.Add("Field3", "2019");
sourceDic1.Add("Field4", "2018_E_" + i);

lSource.Add(sourceDic1);



for (int i = 2; i < 4; i++)

var targetDic2 = new Dictionary<string, object>();
targetDic2.Add("Field1", "2017");
targetDic2.Add("Field2", "2018");
targetDic2.Add("Field3", "2019");
targetDic2.Add("Field4", "2018_E_" + i);

lTarget.Add(targetDic2);



// Results
var DoesNotExistInTarget = new List<Dictionary<string, object>>();
var DoesNotExistInSource = new List<Dictionary<string, object>>();

for (int i = 0; i < 2; i++)

var MissingDic1 = new Dictionary<string, object>();
MissingDic1.Add("Field1", "2017");
MissingDic1.Add("Field2", "2018");
MissingDic1.Add("Field3", "2019");
MissingDic1.Add("Field4", "2018_E_" + i);

DoesNotExistInTarget.Add(MissingDic1);


for (int i = 3; i < 4; i++)

var MissingDic2 = new Dictionary<string, object>();
MissingDic2.Add("Field1", "2017");
MissingDic2.Add("Field2", "2018");
MissingDic2.Add("Field3", "2019");
MissingDic2.Add("Field4", "2018_E_" + i);

DoesNotExistInSource.Add(MissingDic2);











share|improve this question
























  • What do you want this to yield? The differences in the sets?

    – Jlalonde
    Mar 8 at 0:45











  • So you want to find the difference based on the values of dictioary? And the keys will always be there and same (3 keys as shown). Also do you want to find the difference of 1st item in list 1 with 1st item in list 2 and so on or with all the dictionary keys in list 1 with all dictionary keys in list 2? Also will dictionary keys in one list (keys in 1st item in list 1 and 2nd item in list 1) be different?

    – peeyush singh
    Mar 10 at 0:37













0












0








0








I want to compare data from two different lists, a source list & a target list. They are two different IEnumerable<Dictionary<string, object>>



I am running two tests and the result for each test should output the missing records from the target and assign the missing records to a new IEnumerable<Dictionary<string, object>>.



The keys in both Dictionary<string, object> are always the same. However, the length of the list and the values in the dictionary might not be.



List 1:



enter image description hereenter image description hereenter image description here



List 2:



enter image description hereenter image description hereenter image description here



Here is how I tried but it only compares the first key and value of the lists.
I want to know if there are any different values with the same keys in each list.



My Controller:



 [HttpPost]
public ActionResult TestResult(ValidationTestVM model)


model.NotFoundInSource = ServiceLayer.RecordCountResults(sourceQueryResults, targetQueryResults).ToList();

model.NotFoundInTarget = ServiceLayer.RecordCountResults(targetQueryResults, sourceQueryResults).ToList();



service class:



 public static IEnumerable<Dictionary<string, object>> RecordCountResults(IEnumerable<Dictionary<string, object>> source, IEnumerable<Dictionary<string, object>> target)



This does not seem to work correctly. Can someone tell what am I doing wrong?



A dummy method that creates the sample data and showing how I expect to see the returns lists:



 public static void DummyData(IEnumerable<Dictionary<string, object>> source, IEnumerable<Dictionary<string, object>> target)

// Dummy Data
List<Dictionary<string, object>> lSource = source.ToList();
List<Dictionary<string, object>> lTarget = target.ToList();

for (int i = 0; i < 3; i++)

var sourceDic1 = new Dictionary<string, object>();
sourceDic1.Add("Field1", "2017");
sourceDic1.Add("Field2", "2018");
sourceDic1.Add("Field3", "2019");
sourceDic1.Add("Field4", "2018_E_" + i);

lSource.Add(sourceDic1);



for (int i = 2; i < 4; i++)

var targetDic2 = new Dictionary<string, object>();
targetDic2.Add("Field1", "2017");
targetDic2.Add("Field2", "2018");
targetDic2.Add("Field3", "2019");
targetDic2.Add("Field4", "2018_E_" + i);

lTarget.Add(targetDic2);



// Results
var DoesNotExistInTarget = new List<Dictionary<string, object>>();
var DoesNotExistInSource = new List<Dictionary<string, object>>();

for (int i = 0; i < 2; i++)

var MissingDic1 = new Dictionary<string, object>();
MissingDic1.Add("Field1", "2017");
MissingDic1.Add("Field2", "2018");
MissingDic1.Add("Field3", "2019");
MissingDic1.Add("Field4", "2018_E_" + i);

DoesNotExistInTarget.Add(MissingDic1);


for (int i = 3; i < 4; i++)

var MissingDic2 = new Dictionary<string, object>();
MissingDic2.Add("Field1", "2017");
MissingDic2.Add("Field2", "2018");
MissingDic2.Add("Field3", "2019");
MissingDic2.Add("Field4", "2018_E_" + i);

DoesNotExistInSource.Add(MissingDic2);











share|improve this question
















I want to compare data from two different lists, a source list & a target list. They are two different IEnumerable<Dictionary<string, object>>



I am running two tests and the result for each test should output the missing records from the target and assign the missing records to a new IEnumerable<Dictionary<string, object>>.



The keys in both Dictionary<string, object> are always the same. However, the length of the list and the values in the dictionary might not be.



List 1:



enter image description hereenter image description hereenter image description here



List 2:



enter image description hereenter image description hereenter image description here



Here is how I tried but it only compares the first key and value of the lists.
I want to know if there are any different values with the same keys in each list.



My Controller:



 [HttpPost]
public ActionResult TestResult(ValidationTestVM model)


model.NotFoundInSource = ServiceLayer.RecordCountResults(sourceQueryResults, targetQueryResults).ToList();

model.NotFoundInTarget = ServiceLayer.RecordCountResults(targetQueryResults, sourceQueryResults).ToList();



service class:



 public static IEnumerable<Dictionary<string, object>> RecordCountResults(IEnumerable<Dictionary<string, object>> source, IEnumerable<Dictionary<string, object>> target)



This does not seem to work correctly. Can someone tell what am I doing wrong?



A dummy method that creates the sample data and showing how I expect to see the returns lists:



 public static void DummyData(IEnumerable<Dictionary<string, object>> source, IEnumerable<Dictionary<string, object>> target)

// Dummy Data
List<Dictionary<string, object>> lSource = source.ToList();
List<Dictionary<string, object>> lTarget = target.ToList();

for (int i = 0; i < 3; i++)

var sourceDic1 = new Dictionary<string, object>();
sourceDic1.Add("Field1", "2017");
sourceDic1.Add("Field2", "2018");
sourceDic1.Add("Field3", "2019");
sourceDic1.Add("Field4", "2018_E_" + i);

lSource.Add(sourceDic1);



for (int i = 2; i < 4; i++)

var targetDic2 = new Dictionary<string, object>();
targetDic2.Add("Field1", "2017");
targetDic2.Add("Field2", "2018");
targetDic2.Add("Field3", "2019");
targetDic2.Add("Field4", "2018_E_" + i);

lTarget.Add(targetDic2);



// Results
var DoesNotExistInTarget = new List<Dictionary<string, object>>();
var DoesNotExistInSource = new List<Dictionary<string, object>>();

for (int i = 0; i < 2; i++)

var MissingDic1 = new Dictionary<string, object>();
MissingDic1.Add("Field1", "2017");
MissingDic1.Add("Field2", "2018");
MissingDic1.Add("Field3", "2019");
MissingDic1.Add("Field4", "2018_E_" + i);

DoesNotExistInTarget.Add(MissingDic1);


for (int i = 3; i < 4; i++)

var MissingDic2 = new Dictionary<string, object>();
MissingDic2.Add("Field1", "2017");
MissingDic2.Add("Field2", "2018");
MissingDic2.Add("Field3", "2019");
MissingDic2.Add("Field4", "2018_E_" + i);

DoesNotExistInSource.Add(MissingDic2);








c# linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 13 at 19:36







Victor_Tlepshev

















asked Mar 7 at 23:59









Victor_TlepshevVictor_Tlepshev

306316




306316












  • What do you want this to yield? The differences in the sets?

    – Jlalonde
    Mar 8 at 0:45











  • So you want to find the difference based on the values of dictioary? And the keys will always be there and same (3 keys as shown). Also do you want to find the difference of 1st item in list 1 with 1st item in list 2 and so on or with all the dictionary keys in list 1 with all dictionary keys in list 2? Also will dictionary keys in one list (keys in 1st item in list 1 and 2nd item in list 1) be different?

    – peeyush singh
    Mar 10 at 0:37

















  • What do you want this to yield? The differences in the sets?

    – Jlalonde
    Mar 8 at 0:45











  • So you want to find the difference based on the values of dictioary? And the keys will always be there and same (3 keys as shown). Also do you want to find the difference of 1st item in list 1 with 1st item in list 2 and so on or with all the dictionary keys in list 1 with all dictionary keys in list 2? Also will dictionary keys in one list (keys in 1st item in list 1 and 2nd item in list 1) be different?

    – peeyush singh
    Mar 10 at 0:37
















What do you want this to yield? The differences in the sets?

– Jlalonde
Mar 8 at 0:45





What do you want this to yield? The differences in the sets?

– Jlalonde
Mar 8 at 0:45













So you want to find the difference based on the values of dictioary? And the keys will always be there and same (3 keys as shown). Also do you want to find the difference of 1st item in list 1 with 1st item in list 2 and so on or with all the dictionary keys in list 1 with all dictionary keys in list 2? Also will dictionary keys in one list (keys in 1st item in list 1 and 2nd item in list 1) be different?

– peeyush singh
Mar 10 at 0:37





So you want to find the difference based on the values of dictioary? And the keys will always be there and same (3 keys as shown). Also do you want to find the difference of 1st item in list 1 with 1st item in list 2 and so on or with all the dictionary keys in list 1 with all dictionary keys in list 2? Also will dictionary keys in one list (keys in 1st item in list 1 and 2nd item in list 1) be different?

– peeyush singh
Mar 10 at 0:37












1 Answer
1






active

oldest

votes


















1














OK I have updated now my answer according to your question update.



You will need one method that checks if dictionaries are equal.



The one below expects to have same keys in both dictionaries, as you need it, but if it happens to have different keys just add one check before the existing equality check.



I have implemented it as an extension method so it's easier to use it:



public static class DictionaryExtensions

/// <summary>
/// Expected to have 2 dictionaries with same keys
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dict1"></param>
/// <param name="dict2"></param>
/// <returns></returns>
public static bool IsEqual<T>(
this Dictionary<string, T> dict1,
Dictionary<string, T> dict2)

foreach (var keyValuePair in dict1)

if (!keyValuePair.Value.Equals(dict2[keyValuePair.Key]))
return false;


return true;




And then use this code inside your RecordCountResult method:



 public static IEnumerable<Dictionary<string, object>> RecordCountResults(
IEnumerable<Dictionary<string, object>> source,
IEnumerable<Dictionary<string, object>> target)

foreach (var sourceDictionary in source)

var existsInTarget = false;

foreach (var targetDictionary in target)

if (sourceDictionary.IsEqual(targetDictionary))

existsInTarget = true;
break;



if (!existsInTarget)
yield return sourceDictionary;




The idea is that you have to loop first through source dictionaries and for each sourceDictionary check if it has the match in target list. If your sourceDictionary doesn't have a match in target it will be reported.



I didn't want to use Linq in RecordCountResults method because with foreach is more readable and easier to understand.



Also note that I'm using yield return instead of having temp list for a return candidates. If you prefer temp list, feel free to change it.






share|improve this answer

























  • This works well. Thank you

    – Victor_Tlepshev
    Mar 14 at 13:38










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054740%2fhow-to-compare-ienumerabledictionarystring-object-based-on-matching-keys-in%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














OK I have updated now my answer according to your question update.



You will need one method that checks if dictionaries are equal.



The one below expects to have same keys in both dictionaries, as you need it, but if it happens to have different keys just add one check before the existing equality check.



I have implemented it as an extension method so it's easier to use it:



public static class DictionaryExtensions

/// <summary>
/// Expected to have 2 dictionaries with same keys
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dict1"></param>
/// <param name="dict2"></param>
/// <returns></returns>
public static bool IsEqual<T>(
this Dictionary<string, T> dict1,
Dictionary<string, T> dict2)

foreach (var keyValuePair in dict1)

if (!keyValuePair.Value.Equals(dict2[keyValuePair.Key]))
return false;


return true;




And then use this code inside your RecordCountResult method:



 public static IEnumerable<Dictionary<string, object>> RecordCountResults(
IEnumerable<Dictionary<string, object>> source,
IEnumerable<Dictionary<string, object>> target)

foreach (var sourceDictionary in source)

var existsInTarget = false;

foreach (var targetDictionary in target)

if (sourceDictionary.IsEqual(targetDictionary))

existsInTarget = true;
break;



if (!existsInTarget)
yield return sourceDictionary;




The idea is that you have to loop first through source dictionaries and for each sourceDictionary check if it has the match in target list. If your sourceDictionary doesn't have a match in target it will be reported.



I didn't want to use Linq in RecordCountResults method because with foreach is more readable and easier to understand.



Also note that I'm using yield return instead of having temp list for a return candidates. If you prefer temp list, feel free to change it.






share|improve this answer

























  • This works well. Thank you

    – Victor_Tlepshev
    Mar 14 at 13:38















1














OK I have updated now my answer according to your question update.



You will need one method that checks if dictionaries are equal.



The one below expects to have same keys in both dictionaries, as you need it, but if it happens to have different keys just add one check before the existing equality check.



I have implemented it as an extension method so it's easier to use it:



public static class DictionaryExtensions

/// <summary>
/// Expected to have 2 dictionaries with same keys
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dict1"></param>
/// <param name="dict2"></param>
/// <returns></returns>
public static bool IsEqual<T>(
this Dictionary<string, T> dict1,
Dictionary<string, T> dict2)

foreach (var keyValuePair in dict1)

if (!keyValuePair.Value.Equals(dict2[keyValuePair.Key]))
return false;


return true;




And then use this code inside your RecordCountResult method:



 public static IEnumerable<Dictionary<string, object>> RecordCountResults(
IEnumerable<Dictionary<string, object>> source,
IEnumerable<Dictionary<string, object>> target)

foreach (var sourceDictionary in source)

var existsInTarget = false;

foreach (var targetDictionary in target)

if (sourceDictionary.IsEqual(targetDictionary))

existsInTarget = true;
break;



if (!existsInTarget)
yield return sourceDictionary;




The idea is that you have to loop first through source dictionaries and for each sourceDictionary check if it has the match in target list. If your sourceDictionary doesn't have a match in target it will be reported.



I didn't want to use Linq in RecordCountResults method because with foreach is more readable and easier to understand.



Also note that I'm using yield return instead of having temp list for a return candidates. If you prefer temp list, feel free to change it.






share|improve this answer

























  • This works well. Thank you

    – Victor_Tlepshev
    Mar 14 at 13:38













1












1








1







OK I have updated now my answer according to your question update.



You will need one method that checks if dictionaries are equal.



The one below expects to have same keys in both dictionaries, as you need it, but if it happens to have different keys just add one check before the existing equality check.



I have implemented it as an extension method so it's easier to use it:



public static class DictionaryExtensions

/// <summary>
/// Expected to have 2 dictionaries with same keys
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dict1"></param>
/// <param name="dict2"></param>
/// <returns></returns>
public static bool IsEqual<T>(
this Dictionary<string, T> dict1,
Dictionary<string, T> dict2)

foreach (var keyValuePair in dict1)

if (!keyValuePair.Value.Equals(dict2[keyValuePair.Key]))
return false;


return true;




And then use this code inside your RecordCountResult method:



 public static IEnumerable<Dictionary<string, object>> RecordCountResults(
IEnumerable<Dictionary<string, object>> source,
IEnumerable<Dictionary<string, object>> target)

foreach (var sourceDictionary in source)

var existsInTarget = false;

foreach (var targetDictionary in target)

if (sourceDictionary.IsEqual(targetDictionary))

existsInTarget = true;
break;



if (!existsInTarget)
yield return sourceDictionary;




The idea is that you have to loop first through source dictionaries and for each sourceDictionary check if it has the match in target list. If your sourceDictionary doesn't have a match in target it will be reported.



I didn't want to use Linq in RecordCountResults method because with foreach is more readable and easier to understand.



Also note that I'm using yield return instead of having temp list for a return candidates. If you prefer temp list, feel free to change it.






share|improve this answer















OK I have updated now my answer according to your question update.



You will need one method that checks if dictionaries are equal.



The one below expects to have same keys in both dictionaries, as you need it, but if it happens to have different keys just add one check before the existing equality check.



I have implemented it as an extension method so it's easier to use it:



public static class DictionaryExtensions

/// <summary>
/// Expected to have 2 dictionaries with same keys
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dict1"></param>
/// <param name="dict2"></param>
/// <returns></returns>
public static bool IsEqual<T>(
this Dictionary<string, T> dict1,
Dictionary<string, T> dict2)

foreach (var keyValuePair in dict1)

if (!keyValuePair.Value.Equals(dict2[keyValuePair.Key]))
return false;


return true;




And then use this code inside your RecordCountResult method:



 public static IEnumerable<Dictionary<string, object>> RecordCountResults(
IEnumerable<Dictionary<string, object>> source,
IEnumerable<Dictionary<string, object>> target)

foreach (var sourceDictionary in source)

var existsInTarget = false;

foreach (var targetDictionary in target)

if (sourceDictionary.IsEqual(targetDictionary))

existsInTarget = true;
break;



if (!existsInTarget)
yield return sourceDictionary;




The idea is that you have to loop first through source dictionaries and for each sourceDictionary check if it has the match in target list. If your sourceDictionary doesn't have a match in target it will be reported.



I didn't want to use Linq in RecordCountResults method because with foreach is more readable and easier to understand.



Also note that I'm using yield return instead of having temp list for a return candidates. If you prefer temp list, feel free to change it.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 14 at 8:16

























answered Mar 10 at 18:07









IsmarIsmar

696613




696613












  • This works well. Thank you

    – Victor_Tlepshev
    Mar 14 at 13:38

















  • This works well. Thank you

    – Victor_Tlepshev
    Mar 14 at 13:38
















This works well. Thank you

– Victor_Tlepshev
Mar 14 at 13:38





This works well. Thank you

– Victor_Tlepshev
Mar 14 at 13:38



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054740%2fhow-to-compare-ienumerabledictionarystring-object-based-on-matching-keys-in%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived