public Dictionary<string, string> nodeHierarchy { get; set; }
public Dictionary<string, string> Parentnode { get; set; }
}
List<RootObject1> ro = new List<RootObject1>(); RootObject1 ro1 = new RootObject1 { nodeHierarchy = new Dictionary<string, string> { { "l1", "x" },{"l2", "x" } } };
ro.Add(ro1);
Dictionary<string, string> di = new Dictionary<string, string>(); di.Add("l1", "x"); di.Add("l2", "x");
Both dl and .nodeHierarchy are objects of type Dictionary. They will be compared by reference.
I'm guessing that you want to compare the elements of each Dictionary.
Don't have time to test the code but you will need to check that each element of w.nodeHierarchy exists in di and also that there are no element of di that do not exist in w.nodeHierarchy. Something like
Member
16 Points
172 Posts
how to compair a dictionary memeber of a class with an other
Mar 09, 2020 09:07 AM|rajemessage|LINK
public class RootObject1
{
public Dictionary<string, string> nodeHierarchy { get; set; }
public Dictionary<string, string> Parentnode { get; set; }
}
List<RootObject1> ro = new List<RootObject1>();
RootObject1 ro1 = new RootObject1
{
nodeHierarchy = new Dictionary<string, string>
{
{ "l1", "x" },{"l2", "x" }
}
};
ro.Add(ro1);
Dictionary<string, string> di = new Dictionary<string, string>();
di.Add("l1", "x");
di.Add("l2", "x");
var x = ro.Where(w => w.nodeHierarchy == di);
// follwing is not compairing pls suggest
yours scincerly
Participant
1620 Points
927 Posts
Re: how to compair a dictionary memeber of a class with an other
Mar 09, 2020 10:42 AM|PaulTheSmith|LINK
Both dl and .nodeHierarchy are objects of type Dictionary. They will be compared by reference.
I'm guessing that you want to compare the elements of each Dictionary.
Don't have time to test the code but you will need to check that each element of w.nodeHierarchy exists in di and also that there are no element of di that do not exist in w.nodeHierarchy. Something like
ro.Where(w => w.nodeHierarchy.All(nh => di.KeyExists(nh.Key) && di[nh.Key] == nh.Value)) && !di.Any(ele => !nh.KeyExists(ele.Key))
(Can't run the code to check. I think the test is '.KeyExists' but could be wrong)
Contributor
3140 Points
983 Posts
Re: how to compair a dictionary memeber of a class with an other
Mar 10, 2020 02:49 AM|Yang Shen|LINK
Hi rajemessage,
I think the method @PaulTheSmith was talking about should be the Dictionary<TKey,TValue>.ContainsKey(TKey) Method. Use the ContainsKey() to check if a key exists in a dictionary.
And the code could be uppdated to:
Best Regard,
Yang Shen
Participant
1620 Points
927 Posts
Re: how to compair a dictionary memeber of a class with an other
Mar 10, 2020 06:32 AM|PaulTheSmith|LINK
Thanks Yang Shen!