简体   繁体   English

当我更改select子句以选择新模型时,为什么此linq查询失败

[英]Why does this linq query fail when I change the select clause to select new Model

Why does this query work: 为什么此查询有效:

var rooms = from m in db.Rooms
                      where
                      m.FK_HotelID == id
                      select m;

            return View(rooms.ToList());

but this query (which I generated from a program called 'Linqer') seemingly fails? 但是此查询(我从名为“ Linqer”的程序生成的查询)似乎失败了?

var rooms = from m in db.Rooms
                      where
                      m.FK_HotelID == id
                      select new Room
                      {
                          RoomID = m.RoomID,
                          RoomNumber = m.RoomNumber,
                          RoomType = m.RoomType,
                          FK_HotelID = m.FK_HotelID
                      };

            return View(rooms.ToList());

I need help in getting the query to return a List of Room objects. 我需要帮助以获取查询以返回Room对象List I don't think I can use an anonymous type or a DTO Model for the query because the View requires objects of the type Room . 我认为我不能使用匿名类型或DTO模型进行查询,因为View需要使用Room类型的对象。 The query does return results in the Linqer program but when I try to get this view in my VS2013 Project, I get the following error message: 该查询确实在Linqer程序中返回结果,但是当我尝试在VS2013 Project中获取此视图时,出现以下错误消息:

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code. EntityFramework.SqlServer.dll中发生类型为'System.NotSupportedException'的异常,但未在用户代码中处理。

Additional information: The entity or complex type 'HotelApp.Models.Room' cannot be constructed in a LINQ to Entities query. 附加信息:不能在LINQ to Entities查询中构造实体或复杂类型'HotelApp.Models.Room'。

PS, Here is my Room class for reference: 附言,这是我的房间课程供参考:

public partial class Room
{
    public Room()
    {
        Bookings = new HashSet<Booking>();
    }
    public int RoomID { get; set; }
    public int RoomNumber { get; set; }
    public string RoomType { get; set; }
    public int? FK_HotelID { get; set; }
    public virtual ICollection<Booking> Bookings { get; set; }
    public virtual Hotel Hotel { get; set; }
}

Because you are using LINQ To SQL here which means, that query is translated to SQL that has no idea about your custom types. 因为这里使用的是LINQ To SQL ,所以该查询将转换为对您的自定义类型一无所知的SQL

    var rooms = (from m in db.Rooms
                      where
                      m.FK_HotelID == id
                      select m).ToList();

    var roomsVm = from m in rooms select new Room
                      {
                          RoomID = m.RoomID,
                          RoomNumber = m.RoomNumber,
                          RoomType = m.RoomType,
                          FK_HotelID = m.FK_HotelID
                      };

            return View(roomsVm.ToList());

What happens here is: 这里发生的是:

1) The first query goes to SQL and finds all the rooms you need. 1)第一个查询转到SQL,并找到您需要的所有房间。

2) Then it is translated to the List of C# objects. 2)然后将其转换为C#对象的列表。

3) The second query at this point is using LINQ To Objects and therefore can use your custom model. 3)此时的第二个查询正在使用LINQ To Objects ,因此可以使用您的自定义模型。


Better solution for you would be to create a ViewModel that would contain all the fields you need sth like: 更好的解决方案是创建一个ViewModel ,其中包含您需要的所有字段,例如:

public class RoomViewModel {
    public int RoomId {get;set;}
    public string RoomNumber {get;set;}
    public string HotelName {get;set;}
//and all other properties you need on your View
}

then map your DTO to this new ViewModel : 然后将您的DTO映射到这个新的ViewModel

var roomsVm = from m in rooms select new RoomViewModel
                          {
                              RoomId = m.RoomID,
                              RoomNumber = m.RoomNumber,
                              HotelName = m.Hotel.HotelName
                              //and other properties
                          };

                return View(roomsVm.ToList());

That's cause Room is one of your mapped entity and projecting to a mapped entity is not allowed. 这是因为Room是您的映射实体之一,并且不允许投影到映射实体。 You will have to use a DTO or project to an anonymous type. 您将必须使用DTO或项目为匿名类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM