Answer the question
In order to leave comments, you need to log in
How to return the entire dataset from the SqlQuery method?
Good day to all!
I understand web api 2.
Below is a method that returns data in JSON format from the table database, Books. But it only returns 1 row, which is obvious thanks to FirstOrDefaultAsync() .
namespace BookingAapp.Controllers
{
public class ValuesController : ApiController
{ ...
public async Task<Book> GetBook(int id)
{
//Book book = db.Books.Find(id);
string query = "SELECT * FROM Books WHERE Id > @p0";
Book book = await db.Books.SqlQuery(query, id).FirstOrDefaultAsync();
return book;
}
...
}
Answer the question
In order to leave comments, you need to log in
I answer myself.
Rewrote the code a bit, now it returns the entire data set
// GET api/values/5
public IEnumerable<Book> GetBook(int id)
{
DbRawSqlQuery<Book> book = db.Database.SqlQuery<Book>("select * from Books where id >@p0", id);
return book;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question