Using IQueryable in linq query

Introduction

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

Example

Create a new project in visual studio selecting asp.net 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<MultipleFileUploads.Database.author≶
@{
    ViewBag.Title = "Iqurable Implementation";
}

<h2≶Iqurable Implementation</h2≶
<div class="col-lg-12"≶
    <table class="table-bordered"≶
        <thead≶
            <tr≶
                <th≶Author last name</th≶
                <th≶Author first name</th≶
                <th≶City</th≶
                <th≶contract</th≶
            </tr≶
        </thead≶
        <tbody≶
            @foreach (var item in Model)
            {
                <tr≶
                    <td≶@item.au_lname</td≶
                    <td≶@item.au_fname</td≶
                    <td≶@item.city</td≶
                    <td≶@item.contract</td≶
                </tr≶
            }
        </tbody≶
    </table≶
</div≶

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

Result

IQueryable Implementation