无法将类型 'System.Collections.Generic.List' 隐式转换为 'System.Collections.Generic.List<Model.Room
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<Model.Room
尝试 return 实体选择字段但出现错误
我的界面
public interface IRoomRepository
{
List<Room> All();
Room Get(int id);
Room Add(Room obj);
void Delete(Room obj);
void Update(Room obj);
}
我的存储库和我实现了 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();
}
}
出现以下错误
无法将类型 'System.Collections.Generic.List<>' 隐式转换为 'System.Collections.Generic.List'
编辑
房间模型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; }
}
}
您必须明确地创建 Room
对象。new {}
创建无法转换为 Room
的匿名对象。假设 属性 名称相同,以下应该有效
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;
}
}
尝试 return 实体选择字段但出现错误
我的界面
public interface IRoomRepository
{
List<Room> All();
Room Get(int id);
Room Add(Room obj);
void Delete(Room obj);
void Update(Room obj);
}
我的存储库和我实现了 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();
}
}
出现以下错误
无法将类型 'System.Collections.Generic.List<>' 隐式转换为 'System.Collections.Generic.List'
编辑
房间模型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; }
}
}
您必须明确地创建 Room
对象。new {}
创建无法转换为 Room
的匿名对象。假设 属性 名称相同,以下应该有效
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;
}
}