The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Using Parenthesis in a DynamicQuery

Last post 06-27-2008, 4:30 PM by David.Parsons. 0 replies.
Sort Posts: Previous Next
  •  06-27-2008, 4:30 PM 10046

    Using Parenthesis in a DynamicQuery

    The use of DynamicQuery parenthesis is available as of the ES 2008.1.0623.0 release. Here are a couple of examples from our NUnit tests. Notice where the default conjunction changes come in relation to the opening/closing paren. You should be able to conditionally loop or nest things, provided you make sure you have matching opening and closing parens.

    AND with an OR Loop:

    Code:
    EmployeeCollection collection = new EmployeeCollection();
    
    collection.Query.Select(
        collection.Query.EmployeeID,
        collection.Query.Supervisor,
        collection.Query.Age);
    
    collection.Query.Where(collection.Query.Age == 20);
    
    collection.Query.Where(new esWhereItem(esParenthesis.Open));
    
    collection.Query.es.DefaultConjunction = esConjunction.Or;
    
    for (int i = 0; i < 4; i++)
    {
        collection.Query.Where(
            collection.Query.Supervisor == i &
            collection.Query.EmployeeID == i + 1);
    }
    
    collection.Query.Where(new esWhereItem(esParenthesis.Close));
    
    collection.Query.Load();

    collection.Query.es.LastQuery:

    Code:
    SELECT [EmployeeID] AS 'EmployeeID',
        [Supervisor] AS 'Supervisor',[Age] AS 'Age'  
    FROM [ForeignKeyTest].[dbo].[Employee] 
    WHERE [Age] = @Age1 AND 
    (
        ([Supervisor] = @Supervisor2 AND [EmployeeID] = @EmployeeID3)  
        OR ([Supervisor] = @Supervisor4 AND [EmployeeID] = @EmployeeID5) 
        OR ([Supervisor] = @Supervisor6 AND [EmployeeID] = @EmployeeID7) 
        OR ([Supervisor] = @Supervisor8 AND [EmployeeID] = @EmployeeID9)
    )

    Multiple Nesting of AND and OR:

    Code:
    EmployeeCollection collection = new EmployeeCollection();
    
    collection.Query.Select(
        collection.Query.EmployeeID,
        collection.Query.Supervisor,
        collection.Query.Age);
    
    collection.Query.Where(collection.Query.Age == 30);
    
    collection.Query.Where(new esWhereItem(esParenthesis.Open));
    collection.Query.Where(new esWhereItem(esParenthesis.Open));
    
    collection.Query.es.DefaultConjunction = esConjunction.Or;
    
    collection.Query.Where(
        collection.Query.EmployeeID == 1 &
        collection.Query.Supervisor.IsNull());
    collection.Query.Where(
        collection.Query.EmployeeID == 2 &
        collection.Query.Supervisor == 1);
    
    collection.Query.Where(new esWhereItem(esParenthesis.Close));
    collection.Query.es.DefaultConjunction = esConjunction.And;
    collection.Query.Where(new esWhereItem(esParenthesis.Open));
    
    collection.Query.es.DefaultConjunction = esConjunction.Or;
    
    collection.Query.Where(
        collection.Query.LastName == "Smith" &
        collection.Query.Supervisor.IsNull());
    collection.Query.Where(
        collection.Query.LastName == "Jones" &
        collection.Query.Supervisor == 1);
    
    collection.Query.Where(new esWhereItem(esParenthesis.Close));
    collection.Query.Where(new esWhereItem(esParenthesis.Close));
    
    collection.Query.Load();

    collection.Query.es.LastQuery:

    Code:
    SELECT [EmployeeID] AS 'EmployeeID',
        [Supervisor] AS 'Supervisor',[Age] AS 'Age'  
    FROM [ForeignKeyTest].[dbo].[Employee] 
    WHERE [Age] = @Age1 AND 
    (
        (
            ([EmployeeID] = @EmployeeID2 AND [Supervisor] IS NULL) 
            OR ([EmployeeID] = @EmployeeID4 AND [Supervisor] = @Supervisor5)
        ) 
        AND 
        (
            ([LastName] = @LastName6 AND [Supervisor] IS NULL) 
            OR ([LastName] = @LastName8 AND [Supervisor] = @Supervisor9)
        )
    )

    David Neal Parsons
    www.entityspaces.net
View as RSS news feed in XML