2

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;
    }

2 Answers 2

5

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.

10
  • Error: The type or namespace name 'IncByYrMonthModel' could not be found
    – gareching
    Oct 8, 2015 at 21:10
  • @gareching then include it. Just right click on it and the resolve references.
    – Christos
    Oct 8, 2015 at 21:11
  • I just have one concern, I think I need to change year, month and total to Int as I encountered an error "Cannot implicitly convert type 'string' to int,
    – gareching
    Oct 8, 2015 at 21:16
  • @gareching sure thing, this is the correct one. I didn't even notice their types :)
    – Christos
    Oct 8, 2015 at 21:17
  • Question: I haven't run this code yet, but I wanted to make the month in string like "January", "February" Etc.
    – gareching
    Oct 8, 2015 at 21:17
0

You are basically trying to assign an anonymous type

new { d.Key.month, d.Key.year, count = d.Count() }

to your object. Instead of select new { ... }, just select new IncByYrMonthModel() { ... } to populate your object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.