There are some central concepts in LLBLGen which I'll explain with samples in code and equivalent Sql query.
- Predicate, IPredicate and PredicateExpression: These are translated to Where clauses. You can think of PredicateExpression as a complex predicate that's made of several predicates joined with AND and OR.
Note: Treat the code snippets below as pseudo code as I don't have access to LLBLGen right now.
var pred = CustomerFields.Id == 5;
new DataAccessAdapter.FetchEntityCollection(coll, new RelationPredicateBucket(pred));
This will roughly translate to:
SELECT * FROM Customer WHERE Id = 5
You can combine several predicates using PredicateExpression:
var predEx = new PredicateExpression();
predEx.Add(CustomerFields.Id == 5);
predEx.AddWithOr(CustomerFields.Name == "X");
Is equivalent to:
SELECT * FROM Customer WHERE Id = 5 OR Name = 'X'
Relations: Relations represents the relations in your database. There's a Relations property in every Entity in generated code that contains every relation of that entity. For example if you have a Customer table that has a one-to-many relation with your Order table, the corresponding Entities will have a static Relations property that contains those relations:
CustomerEntity.Relations.OrderEntityUsingCustomerId;
OrderEntity.Relations.CustomerEntityUsingCustomerId;
You use these relations when you want to perform a join and return results based on the join. For example if you want to fetch all customers that have an order that's varlue is greater than 50000 you do this:
var pred = OrderFields.Value > 50000;
var rpb = new RelationPredicateBucket();
rpb.PredicateExpression.Add(pred);
rpb.Relations.Add(CustomerEntity.Relations.OrderEntityUsingCustomerId);//perform join
This will translate to:
SELECT C.* FROM Customer AS C JOIN Order AS O ON C.Id = O.CustomerId WHERE O.Value > 50000
For multiple joins you just add more relations, to get customer that have orders with values higher than 50000 that have an OrderDetail that's quantity is more than 1:
var pred = OrderFields.Value > 50000 & OrderDetailFields.Quantity > 1;
var rpb = new RelationPredicateBucket();
rpb.PredicateExpression.Add(pred);
rpb.Relations.Add(CustomerEntity.Relations.OrderEntityUsingCustomerId);//perform customer and order join
rpb.Relations.Add(OrderEntity.Relations.OrderDetailEntityUsingOrderId);//perform order and order detail join
Which produces this sql query:
SELECT C.* FROM Customer AS C JOIN Order AS O ON C.Id = O.CustomerId JOIN OrderDetail AS OD ON OD.OrderId = O.Id WHERE O.Value > 50000 AND OD.Quantity > 1