Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6320651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:00:46+00:00 2026-05-24T16:00:46+00:00

In my ( C# + SQL Server ) application, the user will be able

  • 0

In my (C# + SQL Server) application, the user will be able to define rules over data such as:

ITEM_ID = 1 
OR (ITEM_NAME LIKE 'something' AND ITEM_PRICE > 123 
AND (ITEM_WEIGHT = 456 OR ITEM_HEIGHT < 789))

The set of items to validate will always be different but they are not a huge number. However, the number of rules is high (let’s say, 100000).

How can I check which rules validated (considering also into account performance) a given set of numbers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:00:47+00:00Added an answer on May 24, 2026 at 4:00 pm

    You could use some of Microsoft’s own parsing engine for T-SQL.

    You can find them in the assemblies Microsoft.Data.Schema.ScriptDom.dll and Microsoft.Data.Schema.ScriptDom.Sql.dll.

    TSql100Parser parser = new TSql100Parser(false);
    
    IList<ParseError> errors;
    Expression expr = parser.ParseBooleanExpression(
            new StringReader(condition),
            out errors
        );
    
    if (errors != null && errors.Count > 0)
    {
        // Error handling
        return;
    }
    

    If you don’t get any errors, the string is a valid filter expression. Though there might be some harmful expressions.

    If you wish, you could run the expression trough your own visitor to detect any unwanted constructs (such as sub-queries). But be aware that you would have to override almost all of the 650 overloads, for both Visit(...) and ExplicitVisit(...). Partial classes would be good here.

    When you are satisfied, could then build a complete SELECT statement, with all of the expressions:

    var schemaObject = new SchemaObjectName();
    schemaObject.Identifiers.Add(new Identifier {Value = "MyTable"});
    
    var queryExpression = new QuerySpecification();
    queryExpression.FromClauses.Add(
        new SchemaObjectTableSource {SchemaObject = schemaObject});
    
    // Add the expression from before (repeat as necessary) 
    Literal zeroLiteral = new Literal
    {
        LiteralType = LiteralType.Integer,
        Value = "0",
    };
    Literal oneLiteral = new Literal
    {
        LiteralType = LiteralType.Integer,
        Value = "1",
    };
    WhenClause whenClause = new WhenClause
    {
        WhenExpression = expr, // <-- here
        ThenExpression = oneLiteral,
    };
    CaseExpression caseExpression = new CaseExpression
    {
        ElseExpression = zeroLiteral,
    };
    caseExpression.WhenClauses.Add(whenClause);
    queryExpression.SelectElements.Add(caseExpression);
    
    var selectStatement = new SelectStatement {QueryExpression = queryExpression};
    

    … and turn it all back into a string:

    var generator = new Sql100ScriptGenerator();
    string query;
    generator.GenerateScript(selectStatement, out query);
    
    Console.WriteLine(query);
    

    Output:

    SELECT CASE WHEN ITEM_ID = 1
                     OR (ITEM_NAME LIKE 'something'
                         AND ITEM_PRICE > 123
                         AND (ITEM_WEIGHT = 456
                              OR ITEM_HEIGHT < 789)) THEN 1 ELSE 0 END
    FROM   MyTable
    

    If this expression gets too large to handle, you could always split up the rules into chunks, to run a few at the time.


    Though, to be allowed to redistribute the Microsoft.Data.Schema.ScriptDom.*.dll files, you have to own a licence of Visual Studio Team System (Is this included in at least VS Pro/Ultimate?)

    Link: http://blogs.msdn.com/b/gertd/archive/2008/08/22/redist.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello we have an SQL server application running over a low bandwith connection. We
I`d like to implement a feature, that will allow user to type something like
I'm building SaaS application where back end will be SQL Server and WCF Services.
Im creating an IPad Application which will have an SQL Server backend. My question
I have an application in Asp.net and using sql server to store user information.
I am building a very simple asp.net application that will have a SQL Server
I have an MS SQL server application where I have defined my relationships and
I'm looking to add an advanced search capability to my ASP.NET/SQL Server 2005 application.
So I'm using this SQL Server with my application and I decided to display
t-sql, sql server 2008 My application must check connection status to database every 5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.