Getting a datareader with a filter and sort

Posts   
 
    
daz_oldham avatar
daz_oldham
User
Posts: 62
Joined: 20-Jul-2007
# Posted on: 25-Jul-2007 23:10:35   

Hello

I am trying to figure out how to get a datareader with a filter and sort on them. Also it would be handy to know how to get a filtered entity collection too.

Basically I want to get my datareader based on a couple of parameters, and then loop around it writing certain things out as required.

I can see from the manual how to do the various individual elements, but I'm struggling to see how I would do these all together simple_smile

Many thanks

Darren

daz_oldham avatar
daz_oldham
User
Posts: 62
Joined: 20-Jul-2007
# Posted on: 25-Jul-2007 23:16:54   

Sorry - I'm on LLBL Pro 2.0.0.0 Final with SQL Server 2005 in Adapter mode

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Jul-2007 09:14:42   

Is this example ok?

// C#
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
    ResultsetFields fields = new ResultsetFields(3);
    // simply set the fields in the indexes, which will use the field name for the column name
    fields[0] = CustomersFields.CustomerId;
    fields[1] = CustomersFields.CompanyName;
    fields[2] = OrdersFields.OrderId;
    
    RelationPredicateBucket filter = new RelationPredicateBucket(CustomersFields.Country == "Germany");
    filter.Relations.Add(CustomersEntity.Relations.OrdersEntityUsingCustomerId);

    ISortExpression sorter = new SortExpression();
    sorter.Add(new SortClause(CustomersFields.CompanyName, null, SortOperator.Ascending));

    IDataReader reader = adapter.FetchDataReader(fields, filter, CommandBehavior.CloseConnection, 0, sorter, false);
    while (reader.Read())
    {
        Console.WriteLine("Row: {0} | {1} | {2} |",
            reader.GetValue(0), reader.GetValue(1), reader.GetValue(2));
    }
    reader.Close();
}
David Elizondo | LLBLGen Support Team
daz_oldham avatar
daz_oldham
User
Posts: 62
Joined: 20-Jul-2007
# Posted on: 26-Jul-2007 12:06:14   

Yes - perfect - I even managed to put two predicates in as well - I am getting the hang of it after all wink

Must say LLBL totally overwhelmed me at first, but I'm starting to see the benefits of it now, and it is looking good.

I suppose the next thing I need to look at is how to start adding another layer into my application to communicate with the new DAL to start re-using this code.

Many thanks for your help.

Darren