Yesudass Moses
Yesudass Moses

Reputation: 1859

Entity Framework SQL Query Generation

I am having difficulty understanding how Entity Framework generates a single SQL Query from a couple of Linq methods. Here is a code which selects Username from a table.

        var result1 = context.UserMasters
                             .Where(t => t.UserRole == "LOCAL")
                             .Skip(100)
                             .Take(100)
                             .Select(t => new { t.UserName }); 

Typically the above code is identical to the below code.

        var test = context.UserMasters.Where(t=> t.UserRole == "LOCAL");
        test = test.Skip(100);
        test = test.Take(100);
        var result2 = test.Select(t => new { t.UserName });

I hope we can get results from the table after any Linq Method. So there is a list readily available always. Is it selecting all results from the table initially and doing operation on the list ?

Upvotes: 0

Views: 257

Answers (1)

Akash Veer
Akash Veer

Reputation: 180

If the type is IQueryable, then the filtering of data happens at the database side. But if the type of query is IEnumerable or other than IQueryable, all the data from the table is fetched and filtering is done at .Net side. It is always advised to use IQueryable in these cases. Please read about IQueryable and IEnumerable difference.

Upvotes: 3

Related Questions