简体   繁体   中英

How to Convert a search string into a LINQ Query?

How to Convert a search string into a LINQ Query.

The delimiters are always AND,OR,(,) Say I have search string inputs as:

  1. abc AND xcv OR cvb OR pqr
  2. (abc AND pqr) OR zxc OR try
  3. pqr OR ter AND (asd OR tor)

I want to build a LINQ query based on this. Example Output(for case 3): 1. where value = "pqr" OR value="ter" AND (value="asd" OR value="tor")

How can we achieve these kinds of segregation for a string.

Take a look into System.Linq.Dynamic . Seems to have the functionality you want.

Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Otherwise you can parse the expression yourself and generate an expression tree.

Dynamic LINQ Queries with Expression Trees

https://www.simple-talk.com/dotnet/.net-framework/dynamic-linq-queries-with-expression-trees/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM