0

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 ?

1
  • 3
    Is it selecting all results from the table initially and doing operation on the list ?- no. it builds a query - then executes it once you start accessing the results Commented Feb 4, 2022 at 12:25

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.