Hi I'm new to programming, could you please help me with this one:
I'am getting an error in "To.List()" Error 1 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List
My Code is:
List<IncByYrMonthModel> GroupedList = (from p in incidentsViewModel
group p by new
{
month = p.DateClosed.Value.Month,
year = p.DateClosed.Value.Year
} into d
select new
{
d.Key.month,
d.Key.year,
count = d.Count()
}).ToList();
return GroupedList;
My Model is:
public class IncByYrMonthModel
{
public string Year { get; set; }
public string Month { get; set; }
public string Total { get; set; }
}
Updated code
public List<IncByYrMonthModel> GetYrMonth2(HttpSessionStateBase session, int id, int _StrYear)
{
List<IncidentsModel> incidentsViewModel = ViewModelService.Fetch<List<IncidentsModel>>(session, id);
List<IncByYrMonthModel> GroupedList = (from p in incidentsViewModel
group p by new
{
month = p.DateClosed.Value.Month,
year = p.DateClosed.Value.Year
} into d
select new IncByYrMonthModel
{
Month = d.Key.month,
Year = d.Key.year,
Total = d.Count()
});
return GroupedList;
}
You should just change this
select new { d.Key.month, d.Key.year, count = d.Count() }
to this
select new IncByYrMonthModel { Month = d.Key.month, Year = d.Key.year, Total = d.Count() }
The problem in the first case is that you have a sequence of object of an anonymous type, which you try to convert them to a list of IncByYrMonthModel
, which is impossible.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments