Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How can i translate this string sql query in entityframework/link

$
0
0

Hello,

I want to write the same query in an entityframework context, with linq query syntax.

The problem is not the query itself but the way i build this query. It is easy to concatenate strings in order to build a complex sql filter, but i do not know how to do the same thing in an object context:

String sql = "";

// search string contains a string with this format: "(word1 word2 word3) OR (word4 word5)

foreach (string matches_or in Regex.Split(search, "[ ]+OR[ ]+", RegexOptions.IgnoreCase))

{

    String filter1 = "";

    if (matches_or.StartsWith('(') && matches_or.EndsWith(')'))

    {

        String expression = matches_or.Substring(1, expression.Length- 2);

        

        foreach (string word in Regex.Split(expression, "[ ]+"))

        {

            if (filter1.Length >0)

            {

                filter1 += " AND ";

            }

            filter1 += "name LIKE '%" + word + "%'";

        }

        if (filter1.Length > 0)

        {

            filter1 = "(" + filter1 + ")";

        }

    }

    

    if (sql.Length > 0)

    {

        sql += " OR ";

    }

    sql += filter1;       

}

sql = "SELECT * FROM people WHERE " + sql;

Thanks


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>