Cramarciuc Lucian
Cramarciuc Lucian

Reputation: 5

Cannot implicitly convert type 'System.Linq.IQueryable to System.Data.Entity.DbSet<>

I receive the following error when I try to run my code. I haven't managed to solve it yet, please Help:

edit: Marked with * where it fails. >

public IQueryable<Video> GetVideos([QueryString("id")] int? categorieId)
{
    var _db = new TeleviziuneAcademicaAspSilverlight.Models.VideoContext();
    IQueryable<Video> query = *_db.Videos;*
    if (categorieId.HasValue && categorieId > 0)
    {
        query = query.Where(v => v.CategorieID == categorieId);
    }

    return query;

Upvotes: 0

Views: 712

Answers (2)

Jason P
Jason P

Reputation: 27012

You seem to have two classes called Video. If you need both, you'll need to project from one to the other before your return statement:

return query.Select(dbVideo => new Appname.Video()
    {
        Prop1 = dbVideo.Prop1,
        Prop2 = dbVideo.Prop2,
        // etc.
    });

Though you'll probably need to change the return type to an IEnumerable<> if you do that.

If you can just work with Appname.Models.Video, change IQueryable<Video> to IQueryable<Appname.Models.Video> in the method signature and the method body.

Upvotes: 0

Wiktor Zychla
Wiktor Zychla

Reputation: 48230

Change

IQueryable<Video> query =

to

IQueryable<Appname.Models.Video> query =

The reason for your error is that you have the type Video defined twice and because of using a short type name you accidentally reference not the one Video you should.

Also change it in the method's return value

public IQueryable<Appname.Models.Video> GetVideos( ... )

Upvotes: 1

Related Questions