Linq Query of class PropertyInfo

E

Eric

I'm trying to do a linq query of a class's PropertyInfo[] to retrieve all
properties that are of type nullable datetime.
Unfortunately, it's not working, I never get anything back. I've replaced
DateTime? with <Nullable<DateTime>>
PropertyInfo[] pi = cust.GetType().GetProperties();
List<DateTime?> dateFields = (from prop in pi.OfType<DateTime?>()
where prop == DateTime.MaxValue || prop == DateTime.MinValue
select prop).ToList<DateTime?>();
 
J

Jon Skeet [C# MVP]

I'm trying to do a linq query of a class's PropertyInfo[] to retrieve all
properties that are of type nullable datetime.
Unfortunately, it's not working, I never get anything back.  I've replaced
DateTime? with <Nullable<DateTime>>
PropertyInfo[] pi = cust.GetType().GetProperties();
List<DateTime?> dateFields = (from prop in pi.OfType<DateTime?>()
where prop == DateTime.MaxValue || prop == DateTime.MinValue
select prop).ToList<DateTime?>();

You're using "OfType" a if it's going to ask for the property type -
it's not. That's going to iterate through the list of PropertyInfo
elements and return any PropertyInfo elements which are also DateTime?
elements. Clearly there won't be any. What you want is a Where clause:

where pi.PropertyType == typeof(DateTime?)

but then you next seem to be assuming that "prop" is the actual
DateTime property *value*. It's not - it's the PropertyInfo. You'll
need to call GetValue() to fetch the actual value. Something like
(untested):

var dates = from prop in cust.GetType().GetProperties()
where prop.PropertyType == typeof(DateTime?)
let value = (DateTime?) prop.GetValue(cust, null)
where value == DateTime.MaxValue || value ==
DateTime.MinValue
select value;

Jon
 
E

Eric

Thanks Jon,
That worked.

I'm trying to do a linq query of a class's PropertyInfo[] to retrieve all
properties that are of type nullable datetime.
Unfortunately, it's not working, I never get anything back. I've replaced
DateTime? with <Nullable<DateTime>>
PropertyInfo[] pi = cust.GetType().GetProperties();
List<DateTime?> dateFields = (from prop in pi.OfType<DateTime?>()
where prop == DateTime.MaxValue || prop == DateTime.MinValue
select prop).ToList<DateTime?>();

You're using "OfType" a if it's going to ask for the property type -
it's not. That's going to iterate through the list of PropertyInfo
elements and return any PropertyInfo elements which are also DateTime?
elements. Clearly there won't be any. What you want is a Where clause:

where pi.PropertyType == typeof(DateTime?)

but then you next seem to be assuming that "prop" is the actual
DateTime property *value*. It's not - it's the PropertyInfo. You'll
need to call GetValue() to fetch the actual value. Something like
(untested):

var dates = from prop in cust.GetType().GetProperties()
where prop.PropertyType == typeof(DateTime?)
let value = (DateTime?) prop.GetValue(cust, null)
where value == DateTime.MaxValue || value ==
DateTime.MinValue
select value;

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top