Possible Duplicate:
Optimizing Double foreach loop
I am trying to turn a double foreach loop into a single foreach loop
foreach (KeyValuePair<Int64, MyObject> kvp in originCounts)
{
foreach (KeyValuePair<Int64, MyObject> testkvp in originCounts)
{
//Run Comparison on testkvp ad kvp to see if all elements of object are the same
}
}
And MyObject is defined as such
public class MyObject
{
public string FirstName{ get; set; }
public string LastName{ get; set; }
public int UserID { get; set; }
public string Address { get; set; }
}
Is there anyway to do this using one loop? When they are all the same, I add them to a list of Int64. It is the same dictionary.
If you have a
IEnumberableofKeyValuePairs then you could do something like this:Not sure whether it’s really optimized but, it’s shorter! :-]
Of course, whatever you’re comparing will need to be comparable.