I have following code for selecting latest Preference object’s CreatedDate. It uses FirstOrDefault
to select one Preference object from preferences collection.
DateTime? latestPreferenceDate = preferences.FirstOrDefault() == null ? null : (preferences.FirstOrDefault()).CreatedDate;
How can we specify that the selected object should be the one with the latest Created Date? Also, I need to select the object corresponding to latest date for selecting other properties of this object.
Note: I am looking for a method chaining
approach
CODE
class Program
{
static void Main()
{
Int16? test = null;
string val = Convert.ToString(test);
Collection<Preference> preferences = new Collection<Preference>();
Preference pref1 = new Preference();
pref1.CreatedDate = new DateTime(2011, 11, 11);
pref1.PreferenceCode = 1;
Preference pref2 = new Preference();
pref2.CreatedDate = new DateTime(2012, 12, 12);
pref2.PreferenceCode = 2;
preferences.Add(pref1);
preferences.Add(pref2);
DateTime? latestPreferenceDate = preferences.FirstOrDefault() == null ? null : (preferences.FirstOrDefault()).CreatedDate;
}
}
public class Preference
{
public DateTime? CreatedDate { get; set; }
public int PreferenceCode { get; set; }
}
REFERENCES: