Linq Query of class PropertyInfo

  • Thread starter Thread starter Eric
  • Start date Start date
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?>();
 
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
 
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
 
Back
Top