. NET MVC lambda expression contains method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.