The lambda expression contains method (equivalent to the like in an SQL statement) uses considerations:
It is well known that the Contains method is used when you want to use a similar effect in an SQL statement in the Entityframe Entity Framework. however , there is no specific article to point out the details of the contains method used in the process.
1. The necessary conditions for using the Contains method:
The
contains is equivalent to the like statement in SQL. However, contains is only for data of type string (string). If it is an int equal number lambda expression While using the Entity Framework, when designing the data, if you want to use like, make sure that the database field is a string type (text, varchar, and so on) . otherwise think fuzzy query
2. Contains method correlation method (all equivalent to like in SQL) Note: Why is it that equivalence , because the methods listed below are all methods in the Stirng . Like applications are non-character type, value line can also "):
Contains is equivalent to the like '%key% ' indicating whether the specified string object appears in this string.
EndsWith is equivalent to the like '%key ' to determine whether the end of this string instance matches the specified string.
StartsWith is equivalent to the like ' key% ' to determine whether the beginning of this string instance matches the specified string.
3. When using the Contains method, go to do not forget the non-null judgment:
The reason for writing this article comes from the fact that today, when using contains to make queries, it has been an error (the object reference has not been set to an instance of the object ).
At first, it was not possible to understand the solution, after tracing and debugging finally found in the database if there is a value of NULL.
Why the error, look at the following code, presumably everyone will know why.
string str = NULL;
Str. Contains ("error");//This will be reported that the object reference is not set to an instance of the object. "Wrong
So don't forget to put the code
O.searchkeyword.contains ("Query Criteria")
Overwrite to:
O.searchkeyword! = null && o.searchkeyword.contains (("Query condition")
. NET MVC lambda expression contains method