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

rilly009

Trying to return entity selected fields but getting error

My Interface

public  interface IRoomRepository
{
    List<Room> All();

    Room Get(int id);

    Room Add(Room obj);


    void Delete(Room obj);

    void Update(Room obj);
}

My Repository and i implemented IRoomRepository

public List<Room> All()
    {
        using (HotelEntities db = new HotelEntities())
        {
            var result = from room in db.Rooms
                         select new
                         {
                             room.RoomNumber,
                             room.Id,
                             room.RoomType
                         };
            return result.ToList();
        }
    }

Getting the following error

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

EDIT

Room Model Class

namespace Model
 {
    using System;
    using System.Collections.Generic;

    public partial class Room
    {
        public int Id { get; set; }
        public string RoomNumber { get; set; }
        public Nullable<int> RoomTypeId { get; set; }
        public Nullable<int> RoomStatusId { get; set; }

        public virtual RoomStatus RoomStatus { get; set; }
        public virtual RoomType RoomType { get; set; }
    }
}
tchrikch

You have to create Room object explicitely.new {} creates anonymous object that can't be converted to Room. Assuming property names are the same, following should work

public List<Room> All()
{
    using (HotelEntities db = new HotelEntities())
    {
        var items = from room in db.Rooms
                     select new
                     {
                         room.RoomNumber,
                         room.Id,
                         room.RoomType
                     }.ToList();

        var result = items.Select(i=>
                       new Room  {
                         RoomNumber = i.RoomNumber,
                         Id  = i.Id,
                         RoomType  = i.RoomType
                     }).ToList();
        return result;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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<<anonymous type:>>' to 'System.Collections.Generic.List

From Dev

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

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.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

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 'int?' to 'System.Collections.Generic.List<Model>

From Dev

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

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<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 '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<>' to 'IList<>'. An explicit conversion exists (are you missing a cast?)

From Dev

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

From Dev

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

From Dev

Cannot implicitly convert type 'int' to 'System.Collections.Generic.List<QuickTest.Stock>'

From Dev

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

From Dev

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

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.SqlClient.SqlParameter>' to 'System.Data.SqlClient.SqlParameter'

Related Related

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

    Cannot implicitly convert type System.Collections.Generic.List

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

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

  11. 11

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

  12. 12

    Cannot implicitly convert type 'int?' to 'System.Collections.Generic.List<Model>

  13. 13

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

  14. 14

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

  15. 15

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

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

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

  21. 21

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

  22. 22

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

  23. 23

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

  24. 24

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

  25. 25

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

  26. 26

    Cannot implicitly convert type 'int' to 'System.Collections.Generic.List<QuickTest.Stock>'

  27. 27

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

  28. 28

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

  29. 29

    Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.SqlClient.SqlParameter>' to 'System.Data.SqlClient.SqlParameter'

HotTag

Archive