Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1> in C#

gareching

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

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C# Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<FirstApp.Model.TeamDetails>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<FirstApp.Model.TeamDetails>

From Dev

Cannot implicitly convert type to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

From Dev

Cannot implicitly convert anonymous type to System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.Lis<AnonymousType#1>' to 'System.Collections.Generic.List<AdventureCycle.Models.Product>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:>>' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<T>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<MODEL#1>' to 'System.Collections.Generic.List<Model#2>

From Dev

Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

From Dev

Cannot implicitly convert type 'System.Collections.IList' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type ‘System.Linq.IQueryable<AnonymousType#1>’ to ‘System.Collections.Generic.IEnumerable<SubCateViewModel>’

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<Model.Room

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List

From Dev

Web API Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

From Dev

Cannot Implicitly Convert Type 'System.Collections.Generic.List<>' to 'IList<>'. An explicit conversion exists (are you missing a cast?)

From Dev

Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'

Related Related

  1. 1

    C# Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>'

  2. 2

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

  3. 3

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List

  4. 4

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<FirstApp.Model.TeamDetails>

  5. 5

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<FirstApp.Model.TeamDetails>

  6. 6

    Cannot implicitly convert type to 'System.Collections.Generic.List

  7. 7

    Cannot implicitly convert type 'System.Collections.Generic.List

  8. 8

    Cannot implicitly convert type System.Collections.Generic.List

  9. 9

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

  10. 10

    Cannot implicitly convert anonymous type to System.Collections.Generic.List

  11. 11

    Cannot implicitly convert type 'System.Collections.Generic.Lis<AnonymousType#1>' to 'System.Collections.Generic.List<AdventureCycle.Models.Product>'

  12. 12

    Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:>>' to 'System.Collections.Generic.List

  13. 13

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<T>

  14. 14

    Cannot implicitly convert type 'System.Collections.Generic.List<MODEL#1>' to 'System.Collections.Generic.List<Model#2>

  15. 15

    Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>

  16. 16

    Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

  17. 17

    Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

  18. 18

    Cannot implicitly convert type 'System.Collections.IList' to 'System.Collections.Generic.List

  19. 19

    Cannot implicitly convert type ‘System.Linq.IQueryable<AnonymousType#1>’ to ‘System.Collections.Generic.IEnumerable<SubCateViewModel>’

  20. 20

    Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

  21. 21

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<Model.Room

  22. 22

    Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

  23. 23

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'

  24. 24

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List

  25. 25

    Web API Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:

  26. 26

    Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

  27. 27

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable

  28. 28

    Cannot Implicitly Convert Type 'System.Collections.Generic.List<>' to 'IList<>'. An explicit conversion exists (are you missing a cast?)

  29. 29

    Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'

HotTag

Archive