Saturday, November 19, 2011

Ok, Linq just amazes me....

I needed to initialize a list of new objects, one record for each day of the month selected with that day (int) set in the DateOfResult field.

If you look below, the Range function is the trick here, along with the anonymous type initializer (is that the correct term?)  Just Amazing........  Thank you to Igor Ostrovsky for the help.


public ClientMedicalOutcomeObjective New(bool persist,DateTime MonthYear)
{
 
    ClientMedicalOutcomeObjective newObjective = new ClientMedicalOutcomeObjective();
 
    var resultsForTheWholeMonth = 
        from m in Enumerable.Range(1,DateTime.DaysInMonth(MonthYear.Year,MonthYear.Month))
        select new ClientMedicalOutcomeObjectiveResult()
            {   
                DateOfResult = m,
                ObjectiveResultTypeFk = 0 //The none selected
            };
 
    newObjective.ClientMedicalOutcomeObjectiveResultCollection = 
        new TList<ClientMedicalOutcomeObjectiveResult>resultsForTheWholeMonth.ToList());
            
    this.DeepSave(newObjective);
 
    return newObjective;
}