A
A
ARezvanov2014-10-26 08:24:01
ASP.NET
ARezvanov, 2014-10-26 08:24:01

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

But how to return the entire data set, and not just the first row? Something I can't figure out.
Help who knows.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ARezvanov, 2014-10-26
@ARezvanov

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

Y
Yuri, 2014-10-26
@Gilga

public IEnumerable<Book> GetBooks(int id)
        {
            var books = db.Books.Where(x => x.BookId > id).ToList();                       
            return books;
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question