Using IQueryable in linq query


IQueryable where T is type of your entity. in linq to entity i always prefer to use IQueryable interface. Reason is that there is type of sequence which returned by linq to entity queries. IQueryable implements the IEnumerable that's why there is no difference to work with both interfaces


Create a new project in visual studio selecting mvc. Connect your application with Database using Entity framework data base first approach. This Article will show you how to connect with database using entity framework database first approach.i am using pubs database for demonstration purpose.

Controller code

Now add a new controller and write below action result method

pubsEntities dbcontext=new pubsEntities();
IQueryable Authors=(from au in dbcontext.authors select au).Take(10);
return View(Authors);

i just fetch 10 records from database. here you can see no any other cast or conversion required.we can easily use foreach loop to fetch values from it.

View Code

@model IEnumerable<≶
    ViewBag.Title = "Iqurable Implementation";

<h2≶Iqurable Implementation</h2≶
<div class="col-lg-12"≶
    <table class="table-bordered"≶
                <th≶Author last name</th≶
                <th≶Author first name</th≶
            @foreach (var item in Model)

You need to declare a model in very first line. i use same class as model which generated by entity model. you can clearly see that we successfully iterate our model and display values


IQueryable Implementation