2015-03-19 107 views
1

我正在嘗試遵循以下教程:ASP.NET Web窗體Web窗體和Visual Studio入門(URL :http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/introduction-and-overview)。錯誤:無法將類型'System.Data.Entity.DbSet'隱式轉換爲'System.Linq.IQueryable'

我在我的項目中有其他名字,但想法是一樣的。我正嘗試爲假日公園的客戶創建一個預訂系統。 我所做的一切都與本教程相同,但在出現錯誤時我不知道解決方案。在教程中,我在步驟5.

我得到的錯誤是:不能隱式轉換類型'System.Data.Entity.DbSet(PortOfTroyCustomers.Models.Accommodation)'到'System.Linq.IQueryable(PortOfTroyCustomers.Accommodation )」。一個顯式轉換存在(是否缺少強制轉換?)

這是Accommodation.aspx.cs的代碼(在教程:ProductList.aspx.cs)這是錯誤occurted:

 public partial class Accommodation : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    public IQueryable<Accommodation> GetAccommodations([QueryString("id")] int? sortId) 
    { 
     var _db = new PortOfTroyCustomers.Models.PortOfTroyContext(); 
     IQueryable<Accommodation> query = _db.Accommodations; // This is the spot! 
     if (sortId.HasValue && sortId > 0) 
     { 
      query = query.Where(a => a.SortID == sortId); // and this! 
     } 
     return query; 
    } 

} 

其他代碼:

類住宿代碼(在教程幾乎相同Product.cs):

public class Accommodation 
{ 
    [ScaffoldColumn(false)] 
    public int AccommodationID { get; set; } 

    public short NumberOfGuest { get; set; } 

    public string ImagePath { get; set; } 

    [Display(Name = "Richtprijs per week")] 
    [DataType(DataType.Currency)] // data type op geld zetten voor de double 
    public double? PricePerWeek { get; set; } 

    // foreign key 
    public int? SortID { get; set; } 
    public int? ResortID { get; set; } 

    // relaties leggen 
    public virtual Sort Sort { get; set; } 
    public virtual Resort Resort { get; set; } 
} 

類分類代碼(在教程幾乎相同Category.cs):

public class Sort 
{ 
    [ScaffoldColumn(false)] 
    [Display(Name = "Type")] 
    public int SortID { get; set; } 

    [Required, StringLength(100), Display(Name = "Name")] 
    public string Title { get; set; } 

    [Required, StringLength(10000), DataType(DataType.MultilineText)] 
    public string Description { get; set; } 

    public virtual ICollection<Accommodation> Accommodations { get; set; } 
} 

上下文代碼:

public class PortOfTroyContext : DbContext 
{ 
    public PortOfTroyContext() : base("PortOfTroyCustomers") 
    { 
    } 

    public DbSet<Sort> Sorts { get; set; } 
    public DbSet<Accommodation> Accommodations { get; set; } 
    public DbSet<Resort> Resorts { get; set; } 
    public DbSet<Facility> Facilities { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     // nieuwe koppeltabel tussen Ficilities en Resorts (is namelijk een TNMT relatie) 
     modelBuilder.Entity<Facility>() 
      .HasMany(r => r.Resorts).WithMany(i => i.Facilities) 
      .Map(t => t.MapLeftKey("FicilityID") 
       .MapRightKey("ResortID") 
       .ToTable("FicilityResort")); 
    } 
} 

能否請你幫我這個錯誤?

回答

4

當你說: -

var _db = new PortOfTroyCustomers.Models.PortOfTroyContext(); 

編譯器推斷表達式的類型分配的權利,這就是所謂的在C#中隱式類型。

現在,你想轉讓,你這樣的查詢,我們就離開了它們是不同類型的右側& System.Linq.IQueryableSystem.Data.Entity.DbSet: -

IQueryable<Accommodation> query = _db.Accommodations; 

因此,你需要一個明確的類型轉換是這樣的: -

IQueryable<Accommodation> query = (IQueryable<Accommodation>)_db.Accommodations; 
+0

謝謝!!這工作! :D和toppic:我現在有錯誤:'PortOfTroyCustomers.Accommodation'沒有包含'SortID'的定義,也沒有找到接受'PortOfTroyCustomers.Accommodation'類型的第一個參數的擴展方法'SortID'缺少使用指令或程序集引用?)。這個錯誤由​​SortID查詢= query.Where(a => a.SortID == sortId);'。你知道我該如何解決這個錯誤嗎?我也不知道如何解決這個錯誤。 – Marjolein 2015-03-19 14:00:51

+0

@Mjarolein - 我不知道你爲什麼要將它轉換爲'IQuerable ',而不是試試這個:''List query = _db.Accommodations.ToList();'它應該解決你的問題。 – 2015-03-19 14:13:07

+0

如果我這樣做,我會得到和以前一樣的錯誤。但現在通過** _ db.Accommodations.ToList()**/** SortID **/return ** query ** – Marjolein 2015-03-19 14:31:58

相關問題