Error: Cannot implicitly convert type 'System.Data.Entity.DbSet' to 'System.Linq.IQueryable'

asp.net entity-framework linq webforms

Question

I am trying to follow the following tutorial: ASP.NET Web Forms Getting started with Web Forms and Visual studio (URL: http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/introduction-and-overview).

I have other names in my project but the idea is the same. I am trying to create a Reservations system for customers of a Holiday Park. I do everything the same as the tutorial but I get an error where I don't know the solution of. In the tutorial I am at step 5.

The error I get is: Cannot implicitly convert type 'System.Data.Entity.DbSet(PortOfTroyCustomers.Models.Accommodation)' to 'System.Linq.IQueryable(PortOfTroyCustomers.Accommodation)'. An explicit conversion exists (are you missing a cast?)

This is the code of Accommodation.aspx.cs (In tutorial: ProductList.aspx.cs) This is where the error 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;
    }

}

Other code:

Class Accommodation code (almost the same as Product.cs in the tutorial):

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

Class Sort code (almost the same as Category.cs in the tutorial):

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

Context code:

    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"));
    }
}

Can you please help me with this error??

1
1
3/19/2015 1:02:25 PM

Accepted Answer

When you say:-

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

The compiler infers the type of the expression to the right of the assignment, this is known as implicit type in C#.

Now, you are trying to assign, your query like this, we have System.Data.Entity.DbSet on right & System.Linq.IQueryable on left which are different types:-

IQueryable<Accommodation> query = _db.Accommodations;

Thus, you need an explicit typecast like this:-

IQueryable<Accommodation> query = (IQueryable<Accommodation>)_db.Accommodations;
4
3/19/2015 1:44:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow