Reputation: 2842
I am creating an application with MVC 4. The code requires that it goes and gets data for individuals for each month. I have written the code so that it does that. But the month is not ordered. The final results shows the months are all over the place. I want the months in its proper order January, February....
My model is as follows,
public class NonComplianceData
{
public static IEnumerable<string> Months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
public int InspectorId { get; set; }
public string InspectorName { get; set; }
public IEnumerable<MonthData> FullYearData { get; set; }
}
public class MonthData
{
public string Month { get; set; }
public int TotalAuditsCompleted { get; set; }
public int TotalNoDefects { get; set; }
public decimal NonComplianceRate
{
get
{
if (TotalAuditsCompleted == 0)
{
return 0;
}
else
{
return (TotalNoDefects / (decimal)TotalAuditsCompleted) * 100;
}
}
}
}
Then the code that fills the data is
var inspectorData = context.COESDetails
.Where(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId
&& x.UploadCOESDetails.AuditMonth.Contains(criteria.AuditYear))
.Select(x => x.Inspector)
.Where(y => y.Id != 0)
.Distinct()
.OrderBy(x => x.Firstname)
.Select(ud =>
new NonComplianceData
{
InspectorId = ud.Id,
InspectorName = ud.Firstname + " " + ud.Surname,
FullYearData = NonComplianceData.Months.Select(month => new MonthData
{
Month = month,
TotalAuditsCompleted = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == (month + " " + criteria.AuditYear) && x.InspectorId == ud.Id && x.AuditType != (int)AuditType.NotSelected),
TotalNoDefects = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == (month + " " + criteria.AuditYear) && x.InspectorId == ud.Id && x.AuditType != (int)AuditType.NotSelected && x.COESDetailsCOESDefects.Any())
})
});
so how can I order by month?
I initially thought maybe I have a model like the following
public class MonthDefinitions
{
public string Month { get; set; }
public int Order { get; set; }
}
but then I ran into other issues...
any help would be appreciated
Thanks
Upvotes: 0
Views: 96
Reputation: 2842
One of the guys at my work explained this to me.. he said leave it as
IEnumerable<string> Months = new string[]
{ "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" };
When an array is defined, then it is used as shown. By default no sort is applied, it will be used as it is defined.
However in my code when I was prefilling the data, because I was using Entity frame work, when it was being passed to sql it was doing a sort. So he suggested that I put a toList at the end of my sql call
var inspectorData = context.COESDetails
.Where(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId
&& x.UploadCOESDetails.AuditMonth.Contains(criteria.AuditYear)).ToList()....rest of the code
This kept the months in the order I defined them
Thanks for your help guys
Upvotes: 1
Reputation: 11763
Your problem is that you are using the name to order by, and not the value in which they are.
So what you'll get is :
> April August December February January July June March May November October September
You could either use the array position, or as you suggested, attach a numeric value to each month, and sort by that.
Have a look at this answer, but basically, using something like:
DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture ).Month
Given:
IEnumerable<string> Months = new string[]
{ "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" };
You want:
var sorted_months =
Months.OrderBy(m => DateTime.ParseExact(m, "MMMM", System.Globalization.CultureInfo.CurrentCulture ))
That will give you the months in their correct order
Upvotes: 1