Dynamic Queries

Build queries based on user input.

Use the Sybase.Persistence.Query class to retrieve a list of MBOs.

  1. Specify the where condition used in the dynamic query.
    Query query = new Query();
    
    AttributeTest aTest = new AttributeTest();
    
    aTest.Attribute = "state";
    aTest.Value = "NY";
    aTest.Operator  = AttributeTest.EQUAL;
    query.TestCriteria = aTest;
    
    SortCriteria sort = new SortCriteria();
    sort.Add("lname", SortOrder.ASCENDING);
    sort.Add("fname", SortOrder.ASCENDING);
    query.SetSortCriteria(sort);
  2. Use the FindWithQuery method in the MBO to dynamically retrieve a list of MBOs acccording to the specified attributes.
    GenericList<MyMBO> mbos = MyMBO.FindWithQuery(query);
  3. Use the generated database’s executeQuery method to query multiple MBOs through the use of joins.
    Query query = new Query();
    query.Select("c.fname,c.lname,s.order_date,s.id");
    query.From("Customer", "c");
    query.Join("Sales_order", "s", "s.cust_id", "c.id");
    
    AttributeTest ts = new AttributeTest();
    ts.Attribute = "lname";
    ts.Value = "Smith";
    ts.Operator = AttributeTest.EQUAL;
    
    query.TestCriteria = ts;
    
    QueryResultSet qrs = SUP101DB.ExecuteQuery(query);
    
    while (qrs.Next())
    {
      string fname = qrs.GetString(1);
      string lname = qrs.GetString(2);
      int orderId = qrs.GetInt(4);
       // ...
    }
    
Related concepts
Accessing MBO Data
Related reference
Query APIs