EF SQLite's like statement, born into a charindex solution

Source: Internet
Author: User
Tags sqlite

When using the EF SQLite found that the like statement can not be fully queried, looked at the resulting SQL statement similar to this

(CHARINDEX (@p__linq__2, [extent1].[ Leaguename])) > 0)

Check the information, in SQLite is not support charindex this function, in fact, the solution is very simple, we just have to implement a interceptor, and then replace the SQL statement, and then add to the EF can be, the following is the implementation of interceptor:

private static regex Replaceregex = new regex (@ "\ (\ () (. *?), (. *?)            \) \) > 0\);p rivate void Replacecharindexfunc (DbCommand command) {var flag = false; var text = replaceregex.replace (command.                CommandText, M + = {if (!m.success) return m.value;                Flag = true; var key = M.groups[1].                Value; var name = M.groups[2].                Value; Replace the parameter foreach (dbparameter commandparameter in command. Parameters) {if (Commandparameter.parametername = = key.                        Substring (1)) {Commandparameter.value = $ "%{commandparameter.value}%";                    Break            }} return $ "{name} like {key}";            }); if (flag) command.        CommandText = text; }

The first is to replace all charindex, and then to modify the value of the query. Then we add the interceptor to EF:

Public qiutandb (): Base ("Name=defaultconn")        {            dbinterception.add (new Sqliteinterceptor ());        }

  

EF SQLite's like statement, born into a charindex solution

Related Article

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.