---
title: "PropertyInfo.GetValue unable to compare datetimes?"  
description: "PropertyInfo.GetValue unable to compare datetimes?"  
author: "Anonymous User"  
published: 2014-08-14  
updated: 2014-08-14  
canonical: https://www.mindstick.com/forum/2036/propertyinfo-getvalue-unable-to-compare-datetimes  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# PropertyInfo.GetValue unable to compare datetimes?

I am looping through two [objects](https://www.mindstick.com/forum/145447/what-are-objects) to see if all [items](https://www.mindstick.com/forum/33812/getting-number-of-items-selected-in-uicollectionview-in-ios) within the two objects are the same. After adding a watch at the point where it is return false, I see the same values. The two items being compared are both [datetime](https://www.mindstick.com/forum/12949/how-to-validate-if-a-datetime-field-is-not-null-empty). They have the exact same [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages), but are [returning](https://www.mindstick.com/news/2477/returning-ceo-bob-iger-dismisses-the-disney-apple-sale-as-pure-speculation) false. My [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) is Considering these two items are both datetime, are they causing the issue? If not, is there a better way to [compare](https://www.mindstick.com/forum/160270/what-is-the-compare-data-annotation-used-for-and-how-does-it-work) my two objects?

```
private bool CompareObj(Visit object1, Visit object2){    foreach (var obj1PropertyInfo in object1.GetType().GetProperties())    {        foreach (var obj2PropertyInfo in object2.GetType().GetProperties())        {            if (obj1PropertyInfo.Name == obj2PropertyInfo.Name && obj1PropertyInfo.GetValue(object1, null) != obj2PropertyInfo.GetValue(object2, null))            {                return false;            }        }    }    return true;}
```

## Replies

### Reply by Pravesh Singh

Hi John, You can us linq like this:

return object1

.GetType()

.GetProperties()

.All(x => object.Equals(x.GetValue(object1), x.GetValue(object2));


---

Original Source: https://www.mindstick.com/forum/2036/propertyinfo-getvalue-unable-to-compare-datetimes

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
