1

I created an Entity Framework API Controller and now I am trying to adjust my GetData() method from:

public IQueryable<VIPPreviewTimeSlots> GetData()
{
    return db.Data;
}

to

public IQueryable<VIPPreviewTimeSlots> GetData()
{
    return db.Data.SqlQuery("SELECT a,b,c FROM table INNER JOIN another table....");
}

but I get this error:

Cannot implicitly convert type 'System.Data.Entity.Infrastructure.DbSqlQuery’ to 'System.Linq.IQueryable’

How do I cast this to a IQueryable?

4
  • I would think by adding .AsQueryable() to the end of your return statement would work. However, according to this SO question you cannot. You can change the function signature to return an IEnumerable Commented Nov 9, 2016 at 19:53
  • .AsQueryable() worked! Commented Nov 9, 2016 at 19:56
  • @PaulStoner That SO answer is about calling .AsQueryable on a .SqlQuery. Changing the function signature to an IEnumerable will cause it to no longer be able to be lazily chainable. Everything afterward will then be done client-side instead of server-side. .AsQueryable is the correct answer here (and it actually would be the correct answer on your linked question as well, but it has possible performance implications on .SqlQuery where here it does not. Commented Nov 9, 2016 at 20:13
  • @RobertMcKee Thank you for that input. I, personally felt AsQueryable() would work as I have used it in similar cases, but not with EF. Always appreciate input, Commented Nov 9, 2016 at 20:15

1 Answer 1

3

Try using AsQuearable(). But as I noted in my comment, that can have repercussions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.