Inner join to get other fields?

Posts   
 
    
w3stfa11
User
Posts: 13
Joined: 01-Jul-2010
# Posted on: 01-Jul-2010 18:03:50   

I'm completely new with LLBLGen.

I have a database table with some columns like EmployeeId and ClientId and SetupDate. The Ids match other tables (Employee table and Client table)

I have a datagrid containing several columns like PersonId and ClientName, ClientPhone, ClientAddress, etc.

Given the EmployeeId, I need to retrieve the the client information and attach it to a grid.datasource.

Any idea how I can do this?

PersonClientCollection p = new PersonIdCollection();

            p.GetMulti(new PredicateExpression() { });
            
            rgridBlackList.DataSource = p;

This only gives me the relationship table. with the Person and Client Ids. I need to use these to get the Client information (phone, name, addr, etc)

I've been using stored procedures meanwhile but maybe there's a better way.


// in stored proc
SELECT *
FROM [client].[List] abl 
    INNER JOIN [client].ClientGroup cg ON cg.ClientGroupId = abl.ClientGroupId

// in code
DataTable dt=RetrievalProcedures.GetEmployeeNote(EmployeeId);
                rgridNotes.DataSource = dt;
MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 01-Jul-2010 21:21:09   

There are two seperate, but related concepts that you need to get your head around.

The first of these is PreFetch paths. These allow you to fetch an entity (ie one row from a table) and along with it, all of its related entities from other tables.

To use the classic example, suppose you want to fetch a customer, along with each of his orders, and for each order, the collection of order lines.

Customer |->Orders |>OrderLines

This would look something like this.


IPrefetchPath prefetchPath = new PrefetchPath((int)EntityType.CustomerEntity);
prefetchPath.Add(CustomerEntity.PrefetchPathOrders).SubPath.Add(OrderEntity.PreFetchPathOrderLines);
CustomerEntity customer = new CustomerEntity("BLONP", prefetchPath);

The second concept is that of Relations. These allow you to effectivley specify SQL Joins. In the example above, if you wanted to fetch a flattened list consisting of 1 row for each order line, with related, and repeated, columns for the Order and Customer information, you could use a Dynamic List.

This would look something like

ResultsetFields fields = new ResultsetFields(3);
fields.DefineField(CustomerFields.FirstName, 0);
fields.DefineField(OrderFields.InvoiceNumber, 1);
fields.DefineField(OrderLineFields.ProductCode, 2);
IRelationCollection relations = new RelationCollection();
relations.Add(CustomerEntity.Relations.OrderEntityUsingCustomerId);
relations.Add(OrderEntity.Relations.OrderLineEntityUsingOrderId);


DataTable dynamicList = new DataTable();
TypedListDAO dao = new TypedListDAO();
dao.GetMultiAsDataTable(fields, dynamicList, 0, null, null, relations};

Hopefully this gives you a better understanding of what can be acheived with LLBLGen. Feel free to come back to us with any further questions.

Matt