This article is a mirror article of machine translation, please click here to jump to the original article.

View: 35720|Reply: 2

[Source] The difference between Expression<Func<T, bool>> and Func<T, bool> in EF

[Copy link]
Posted on 9/26/2021 9:21:21 AM | | | |
Func<TObject, bool> is a delegate

Expression<Func<TObject, bool>> are expressions

Expression will become a delegate after compilation before it can be run. Like what

Expression<Func<int, bool>> ex = x=>x < 100;

Func<int, bool> func = ex. Compile();

Then you can call func:

func(5) //-returns true

func(200) //- returns false

Expressions cannot be called directly.

Case:Incorrect query code causes a full table query of the database

The error code is as follows:

Pass a variable of type Func as an argument to the Count method,EF queries all the data in the entire table and filters it from memory。 The generated SQL statement looks like this:

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [c]. [id], [c]. [client_type], [c]. [status], [c]. [tenant]
      FROM [table] AS [c]
The correct code is as follows:

The correct writing adds a where condition from the database and then returns the value of the count directly。 The generated SQL statement looks like this:

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT COUNT(*)
      FROM [table] AS [c]
      WHERE [c]. [client_type] = 1


EF Core Series 4 looks at the generated SQL statements
https://www.itsvse.com/thread-9564-1-1.html
ef displays the generated SQL statements
https://www.itsvse.com/thread-3813-1-1.html

Using Func delegate calls areSystem.Linq.EnumerableCount below

The expression called with Expression<Func<T, bool>> isSystem.Linq.QueryableCount below



(End)





Previous:[Practice] Make a green no-install version of Java JDK Windows
Next:Java sends SMS verification codes through Alibaba Cloud SMS SDK
Posted on 9/26/2021 4:19:52 PM |
learned
Posted on 9/26/2021 5:31:25 PM |
Yes, I'm here to learn again...
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com