Does a call to Select () or GroupBy () in Linq call objects that initiate a database query?

It's hard for me to say which operations in linq cause the SQL command to be sent to the database.

I know that calling ToList () or iterating w / foreach will trigger the query, but if Select and GroupBy cause code to execute in the database?

+7
linq linq-to-entities entity-framework
source share
3 answers

No, this is not so, if they correctly call IQueryable and not IEnumerable , they are compiled as expressions and will later be translated into SQL.

You can use the intellisense tooltip to see what the current method will be. If the first parameter to the extension method is IEnumerable and not IQueryable , you will run the database query.

+6
source share

No, Select , GroupBy and most other methods will not invoke a database query. A database query will usually only be executed if you are doing something that requires the results to be known, such as calling Count or ToList , as you mentioned.

To help you see when database queries are being executed, this can help register them. Then, when you look at the code, you can see when the request is sent.

+1
source share

No, the calls to Select () and GroupBy () will not get into the database. Only when the actual result is required (when enumerating using ToList (), ToArray (), Count (), etc.), the Request will be executed against the database.

0
source share

All Articles