Anirudh C
Anirudh C

Reputation: 37

How to convert a search string to a boolean condition?

I have a search criteria stored in a string:

string Searchstr = "(r.Value.Contains("PwC") || (r.Value.Contains("Canadian") && r.Value.Contains("thrive"))) || (r.Value.Contains("Banana") && r.Value.Contains("Gayle"))"

I want to use this in a If statement to check the values:

if(searchstr)
{
  then do this....
}

but the if should have a searchstr as boolean.

How to convert this to boolean?

EDIT: The requirement is to give search criteria dynamically in a text box in the following format - "PwC OR (Canadian AND thrive)". Which will be used to search an XML file.

Therefore I have loaded an XML file and want to have a Where condition in LINQ for which I need to use Dynamic LINQ but string is not allowed in that and also I have some braces to deal with.

Thinking of that I have taken the resultset from the XML(The tag value which i need to search)

var selectedBook = from r in document.Root.Descendants("Archives").Elements("Headline").Elements("Para")
                                   select r;

and would ideally like to try something like:

var query=selectedbook.Where(searchstr)

OR

if(searchstr){....then do this}

Upvotes: 1

Views: 1298

Answers (4)

Lorenzo Dematté
Lorenzo Dematté

Reputation: 7849

It seems like a good scenario for http://scriptcs.net/

Upvotes: 0

Amit Mittal
Amit Mittal

Reputation: 2736

Well as you may guess it is not going to be straight forward but at the same time it is not as hard a problem as it seems

You can perform a few steps to get what you want:

  1. Get the search expression as input (for e.g. "PwC OR (Canadian AND thrive)")

  2. Write an extension method on XElement that returns true and takes the search criteria as input. You will then be able to use

    var selectedBook = from r in 
    document.Root.Descendants("Archives").Elements("Headline").Elements("Para")
    where r.SatisfiesCriteria(searchCriteria)
    select r;
    
  3. Write a parser class that parses searchCritera and stores it in parsed format. (for e.g. you can convert it into postfix notation). This is quite easy and you can use standard algorithm for this. For your purpose OR, AND will be operators and PwC etc. will be operands. Parenthesis will get removed as part of parsing.

  4. Now simply invoke this parser from with in your extension method and then evaluate the postfix expression you get. This again can be done through standard stack based evaluation. Infact it would be better if you parse the criteria once and then only evaluate in where. While evaluating you need to replace the operands with r.Value.Contains

Upvotes: 0

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29233

I'm assuming the string is going to reference only a specific set of objects (r or r.Value in this case, for example - or anything else you want, as long as you know it beforehand). If this is the case, then:

  1. Create a delegate that takes the objects (that may be referenced) as parameters and returns a bool, as you want.
  2. Programmatically write a small C# source file in memory that defines the query as the body of a method (with a fixed name, preferably) that conforms to the delegate specified above.
  3. Use the CSharpCodeProvider class to compile an assembly with your custom function that returns the bool you want.
  4. Run the dynamically written and compiled code from your main program.

Upvotes: 0

Rikalous
Rikalous

Reputation: 4574

You will need to do a bit of work to make this happen, but it is possible.

You should have a look at the dynamic LINQ library. This allows you to specify LINQ conditions (and other clauses) as strings and execute them just like LINQ operators.

Start with the explanation on ScottGu's blog and follow the links: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Upvotes: 1

Related Questions