I have a list of objects that I am trying to bind to a listview. I am sorting by two properties. The problem exists whereby some records may not have one of the properties. This is causing an error. I would like it to still bind the records that have the property.
IEnumerable<ERec> list = retailerList.Cast<ERec>();
lvwRetailStores.DataSource = list.OrderByDescending(r => r.Properties["RS_Partner Type"].ToString())
.ThenBy(r => r.Properties["RS_Title"].ToString());
list.Where(r => r.Properties["RS_Partner_Type"] != null && r.Properties["RS_Title"] != null)
.OrderByDescending(r => r.Properties["RS_Partner Type"].ToString())
.ThenBy(r => r.Properties["RS_Title"].ToString());
Or instead of != null, use whatever test the Properties collection has.
I've found that the ?? Operator works well. I use Parenthesis to evaluate for null,
For Example:
Datetime? Today = DateTimeValue
// Check for Null, if Null put Today's date
datetime GoodDate = Today ?? DateTime.Now
This same logic works in Lambda, just use parenthesis to ensure that the correct comparisons are used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With