none
boolean expression evaluation

    Question

  • Hi, is there any better way to determine whether the expression is true than putting it into the DataTable and perform Select?

    Currently solving like this, but seems quite expensive to me:

    string var = "2";
    DataTable eval = new DataTable();
    eval.Columns.Add("var", typeof(string));
    eval.Rows.Add(new object[1] {var});

    string expr = ">1";
    return (eval.Select("var" + expr).Length > 0)

    Tuesday, March 21, 2006 7:11 PM

Answers

  • Are you trying to create dynamic code ?

    You could juste convert them to integer

    return Int32.Parse("2") > Int32.Parse("1");

     

    If you want dynamic code you can use the J# function eval (I don't know if there is one in C#)

    Tuesday, March 21, 2006 7:45 PM
  • http://www.west-wind.com/presentations/DynamicCode/DynamicCode.htm discusses a dynamic code generation solution that can solve this problem.

    Cheers,

    Greg

    Tuesday, March 21, 2006 10:02 PM
  • Then your current solution sounds good for me, do you experience any bottlenecks?
    Friday, March 24, 2006 1:20 PM
    Moderator
  • They are not really so expensive, but it depends on how you use things. If you want to know if two string values are the same you normally use the == operator. This is not expencive but if you need to lookup 50.000 string's you can better use the Equals method and you will win some time.

    But i don't know what you are doing, you want to check if some values are correct?
    Friday, March 24, 2006 1:43 PM
    Moderator

All replies

  • Are you trying to create dynamic code ?

    You could juste convert them to integer

    return Int32.Parse("2") > Int32.Parse("1");

     

    If you want dynamic code you can use the J# function eval (I don't know if there is one in C#)

    Tuesday, March 21, 2006 7:45 PM
  • http://www.west-wind.com/presentations/DynamicCode/DynamicCode.htm discusses a dynamic code generation solution that can solve this problem.

    Cheers,

    Greg

    Tuesday, March 21, 2006 10:02 PM
  • ThE_lOtUs: No, the operator is also a parameter.

    Greg Young: You saw my 4-lines solution. Dynamic code would of course solve the problem, but...are you sure it is better and simpler? It will require a lot of time to get it working, it would by much much more complicated. That is somehow my problem, but - if I'm right, my application will then require full trust, won't it?
    This is not the right way I think, I need so few - to check whether group of strings match the criteria. If I'd use dynamic code, I need to have language valid code, and this 1) I cannot request from common user, 2) is not secure at all, 3) would get complicated if type conversion is required - using Select, I don't care, "2"<3 passes - as easy as "HI" LIKE "H_".

    The more I think about it the more I am afraid that no solution would be better. Just asking if the capabilities of Select can be accessed without creating the DataTable....

    Thursday, March 23, 2006 5:28 PM
  • There is nothing wrong with your solution.

    You can iterate over all rows yourself, but this wouldn't be any faster. Only if the dataset is really large and you only want to know if a one or more values are greater then 1 you can use something like this:


    int columnIndex = table.Columns.IndexOf( "var" );

    foreach(DataRow row in table.Rows)
    {
        int fieldValue = (int)row[ columnIndex ];
       
        if( fieldValue > 1 )
        {
            return true;
        }
    }

    return false;

     

    Friday, March 24, 2006 12:49 PM
    Moderator
  • Hi,
     thank you for your reply. The thing I'm trying to accomplish is not so easy. As I previous said, the operator is also parameter, so this code wouldn't be useful. Moreover, the type of data in the table I used is string.

     This results in select query like  1 < "2" , which, however, makes no problem to this method.

     I have an array of string values and table of simple conditions they need to pass through, like:

      {"var1","var2","var3","var4","var5","var6"} - vars names
      {"1", "2", "2", "0", "mother", "4"} - vars input
      {"<4", "<4", ">=0", "<>1", "LIKE 'm%'", "-4=0"} - conditions input

    Now, I am to compare whether the vars input matches the conditions. If, I return passed and try another set of conditions input.

    So what I do is create a DataTable, with columns named according to vars names, add one row and fills it with the vars input values.

    Then I go accross the conditions arrays and with each array I build up the query, like

    (var1<4) AND (var2<4) AND (var3>=0) AND (var4<>1) AND (var5 LIKE 'm%') AND (var6-4=0)

    I check whether Select method returns any row and if, I return true.

     

    This works. Just wondering about standard solution in that cases, because I'm using DataTable while my code does not deal with any database, about performance and so on.

    Friday, March 24, 2006 1:10 PM
  • Then your current solution sounds good for me, do you experience any bottlenecks?
    Friday, March 24, 2006 1:20 PM
    Moderator
  • No, I only thought that validating variables against user defined filters is common and I am doing it very strange way, or I miss some function which is generally accessible to do it.

    I also feel quite incomfortable when creating (from my point of view large and thus expensive) objects like DataTable, DataSet and so on "just for fun" for reasons they weren't written. ...how actually expensive is to create such objects?

    Friday, March 24, 2006 1:30 PM
  • They are not really so expensive, but it depends on how you use things. If you want to know if two string values are the same you normally use the == operator. This is not expencive but if you need to lookup 50.000 string's you can better use the Equals method and you will win some time.

    But i don't know what you are doing, you want to check if some values are correct?
    Friday, March 24, 2006 1:43 PM
    Moderator
  • I think I can mark this thread as answered.

    If you are interested in what I'm trying to do in real-world, I have case table:

    result/index 0 1 2
    case1 =1 <4 >=0
    case2 =1 <4 LIKE '%free%'
    case3 =2 =3 <NULL>
    case4 =2 <NULL> <NULL>

    And I have an array of strings (index = cases column name). I'm constructing the "filter" table with one row of items from the array and iterating through the cases table and if Select does not return the "filter" row, the case is disqualified.

    (In fact, the array does not contain any values at the begining and based on the case table I return the index which should be supplied (by user) in order to minimize the number of values needed - eg. If user enters [0]="2", I do not care about [2].)

    Saturday, March 25, 2006 7:16 PM