Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IQueryable does not contain ToList()

Tags:

c#

linq

My view's model is an IEnumerable<SomeModel> so in my controller I do this:

[HttpGet]
public PartialViewResult show() {
    IQueryable query= from data in entity.dbCtxt
        select data;
    return PartialView("show", query.ToList());
}

Turns out IQueryable does not contain the definition of ToList()...

public interface IQueryable : IEnumerable{
     Type ElementType { get; }
     Expression Expression { get; }
     IQueryProvider Provider { get; }
}

How do I cast IQueryable into a list?

like image 996
Dylan Czenski Avatar asked Dec 24 '22 04:12

Dylan Czenski


1 Answers

Because ToList is an extension method of IQueryable<T>, not IQueryable. if that is your query, you can simply do this:

public PartialViewResult show() {

        return PartialView("show", dbCtxt.YourDbSet.ToList());
}

DbSet<T> class inherits from IQueryable<T>, so if you idea is to fetch all the rows of that table, you can call ToList directly from your DbSet.

like image 95
octavioccl Avatar answered Feb 01 '23 11:02

octavioccl