Using the TypedList classes

LLBLGen Pro supports read-only lists based on a selection of entity fields from one or more entities with a relationship, called a TypedList. This section discusses how to generate typed lists as either a Typed Datatable or a POCO class and how to fetch the typed views at runtime.

Choosing the Typed List class type

Which class is generated for a Typed List is determined by its OutputType setting. This setting is found on the Typed List's Code gen. Info tab in the designer and its default value is obtained from the TypedListOutputTypeDefault setting located in the LLBLGen Pro framework section of the Project Settings. There are three values to choose from:

  • TypedDataTable (default)
  • PocoWithLinqQuery
  • PocoWithQuerySpecQuery

The default is TypedDataTable, which will result in what has been the case since the beginning: a DataTable-derived class which is retrievable using the low-level api. The other two are both resulting in a class, typedViewNameRow, and a query to fetch the typed list, either in Linq or in QuerySpec. 

It's recommended to use the TypedListOutputTypeDefault setting to set the output type of your typed lists in one go. You can also set the OutputType setting on individual typed lists in bulk using the Bulk Element Setting Manipulator in the designer.

Instantiating and using a TypedList

Depending on the OutputType chosen for a given typed list, fetching a typed list is done as follows

OutputType: TypedDataTable

Using a TypedList is similar to using a TypedView, both will be generated as typed DataTables.  To fetch a TypedList, the DataAccessAdapter.FetchTypedList() method is used.

In the example below, we'll use a TypedList created from the entities Customer and Order and include the following fields in the resultset: Order.OrderID, Order.OrderDate, Order.ShippedDate, Customer.CustomerID, Customer.CompanyName.

You can filter on any field in Order or Customer or both. Also, you can sort on any field in Order or Customer or both. Let's filter this TypedList on all orders from customers from 'Brazil', and sort the list on the field Order.Freight, ascending. The TypedList is called OrderCustomer.

OrderCustomerTypedList orderCustomer = new OrderCustomerTypedList();
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
    PredicateExpression additionalFilter = new PredicateExpression(CustomerFields.Country == "Brazil");
    ISortExpression sorter = new SortExpression(OrderFields.OrderId | SortOperator.Ascending);
    adapter.FetchTypedList(orderCustomer, additionalFilter, 0, sorter, false);
}
Dim orderCustomer As New OrderCustomerTypedList()
Using adapter As New DataAccessAdapter()
    Dim additionalFilter As new PredicateExpression(CustomerFields.Country = "Brazil")
    Dim sorter As ISortExpression = New SortExpression(OrderFields.OrderId Or SortOperator.Ascending)
    adapter.FetchTypedList(orderCustomer, additionalFilter, 0, sorter, False)
End Using

The TypedList object is now filled with the rows for the 5 columns we've specified in the TypedList editor, sorted on Order.Freight ascending and filtered on Customer.Country equals "Brazil".

Info

If you're using one of the FetchTypedList overloads of DataAccessAdapter which accepts IEntityFields2 and an IRelationPredicateBucket, you have to pass the object you get from typedlist.GetFieldsInfo() as the fieldCollectionToFetch value and the object you get from typedlist.GetRelationInfo() as the filterBucket parameter. Otherwise relations of the TypedList aren't detected. If you don't want to filter the typedlist, use the overloads of FetchTypedList which accept a typedListToFill parameter. See the reference manual for details.

When you need to add additional relationships to the filter because you want to filter on entity data related to entities in the TypedList, you need to use the more low-level overloads of FetchTypedList. A TypedList contains code to help you construct the RelationPredicateBucket objects for filtering.

Use the GetRelationInfo() method of the TypedList object to get an initial bucket with essential information for fetching the data of the TypedList. Because RelationPredicateBucket objects contain an already existing PredicateExpression object you can start adding filter predicates to the bucket as well as the extra relationship objects.

To fetch the TypedList in this case, use the overload which accepts entity fields. See for details about the DataAccessAdapterBase.FetchTypedList methods the LLBLGen Pro runtime framework reference manual.

OutputType: PocoWithLinqQuery

When choosing PocoWithLinqQuery as the OutputType of a typed list, a Linq query is generated into the LinqMetaData class in the form of a method: GetTypedListNameTypedList(). This method returns an IQueryable<T> where T is the type of the POCO class generated for the typed list: TypedListNameRow.

To fetch the query, one has to enumerate the returned IQuerable<T>, as is always the case with Linq. The IQueryable<T> can be appended with joins, where calls and e.g. order by calls to further shape the query.

OutputType: PocoWithQuerySpecQuery

For the QuerySpec variant it's similar to the PocoWithLinqQuery variant: the same class as with the Linq variant is generated: TypedListNameRow, and a method is added to the QueryFactory class for QuerySpec, called GetTypedListNameTypedList() which returns a DynamicQuery<T>, where T is the name of the class generated for the TypedList, TypedListNameRow.

To fetch the query, one has to use one of the usual QuerySpec fetch methods for fetching a DynamicQuery<T> . The returned DynamicQuery<T> can be further configured by appending where clauses, order by clauses like one would do with a normal DynamicQuery<T> query.