Querying for a set of entities and fetch them into an EntityCollection<T> can be done in several ways. Below we'll fetch all Customer instances which match the filter Country=='Germany'. This is illustrated using three different queries: one using our low-level native Api, one using our QuerySpec API and one using Linq to LLBLGen Pro. When using QuerySpec or Linq to LLBLGen Pro, be sure to have the correct namespaces included in your code file.
// QuerySpec, C# var customers = new EntityCollection<CustomerEntity>(); using(var adapter = new DataAccessAdapter()) { var qf = new QueryFactory(); var q = qf.Customer.Where(CustomerFields.Country == "Germany"); adapter.FetchQuery(q, customers); }
' QuerySpec, VB.NET Dim customers As New EntityCollection(Of CustomerEntity)() Using adapter As New DataAccessAdapter() Dim qf As new QueryFactory() Dim q As qf.Customer.Where(CustomerFields.Country = "Germany") adapter.FetchQuery(q, customers) End Using
// Linq, C# IEntityCollection2 customers; using(var adapter = new DataAccessAdapter()) { var metaData = new LinqMetaData(adapter); var q = from c in metaData.Customer where c.Country == "Germany" select c; customers = ((ILLBLGenProQuery)q).Execute<EntityCollection<CustomerEntity>>(); }
' Linq, VB.NET
Dim customers As IEntityCollection2
Using adapter As New DataAccessAdapter()
Dim metaData As new LinqMetaData(adapter)
Dim q = From c in metaData.Customer Where c.Country = "Germany" Select c
customers = CType(q, ILLBLGenProQuery).Execute(Of EntityCollection(Of CustomerEntity))()
End Using
// Low-level API, C# EntityCollection<CustomerEntity> customers = new EntityCollection<CustomerEntity>(); RelationPredicateBucket filter = new RelationPredicateBucket(CustomerFields.Country=="Germany"); using(DataAccessAdapter adapter = new DataAccessAdapter()) { adapter.FetchEntityCollection(customers, filter); }
' Low-level API, VB.NET
Dim customers As New EntityCollection(Of CustomerEntity)()
Dim filter As New RelationPredicateBucket(CustomerFields.Country="Germany")
Using adapter As new DataAccessAdapter()
adapter.FetchEntityCollection(customers, filter)
End Using
In the above examples, the collection fetched is created outside the using block with the adapter. For Linq and QuerySpec the fetch methods which return typed collection objects are used. See for alternatives and other ways to fetch collections using a query the sections for the particular query system, e.g. Linq to LLBLGen Pro or QuerySpec.
// QuerySpec, C#, uses EXISTS query. using(var adapter = new DataAccessAdapter()) { var qf = new QueryFactory(); var q = qf.Order .Where(qf.Customer .CorrelatedOver(OrderEntity.Relations.CustomerEntityUsingCustomerId) .Contains(myCustomer)); adapter.FetchQuery(q, myCustomer.Orders); } // or, using direct FK filter: using(var adapter = new DataAccessAdapter()) { var qf = new QueryFactory(); var q = qf.Order.Where(OrderFields.CustomerId == myCustomer.CustomerId); adapter.FetchQuery(q, myCustomer.Orders); } // or, using a join using(var adapter = new DataAccessAdapter()) { var qf = new QueryFactory(); var q = qf.Order .From(QueryTarget.InnerJoin(OrderEntity.Relations.CustomerEntityUsingCustomerId)) .Where(CustomerFields.CustomerId==myCustomer.CustomerId); adapter.FetchQuery(q, myCustomer.Orders); }
' QuerySpec, VB.NET, uses EXISTS query. Using adapter As New DataAccessAdapter() Dim qf As new QueryFactory() Dim q = qf.Order _ .Where(qf.Customer _ .CorrelatedOver(OrderEntity.Relations.CustomerEntityUsingCustomerId) _ .Contains(myCustomer)) adapter.FetchQuery(q, myCustomer.Orders) End Using ' or, using direct FK filter: Using adapter As New DataAccessAdapter() Dim qf As new QueryFactory() Dim q = qf.Order.Where(OrderFields.CustomerId = myCustomer.CustomerId) adapter.FetchQuery(q, myCustomer.Orders) End Using ' or, using a join Using adapter As New DataAccessAdapter() Dim qf As new QueryFactory() Dim q = qf.Order _ .From(QueryTarget.InnerJoin(OrderEntity.Relations.CustomerEntityUsingCustomerId)) _ .Where(CustomerFields.CustomerId=myCustomer.CustomerId) adapter.FetchQuery(q, myCustomer.Orders) End Using
// Low-level API, C#, will use JOIN query. using(DataAccessAdapter adapter = new DataAccessAdapter()) { // Use helper method to produce RelationPredicateBucket with relation + predicate. // You adjust this object with additional filters or create your own. adapter.FetchEntityCollection(myCustomer.Orders, myCustomer.GetRelationInfoOrders()); }
' Low-level API, VB.NET Using adapter As New DataAccessAdapter() ' Use helper method to produce RelationPredicateBucket with relation + predicate. ' You adjust this object with additional filters or create your own. adapter.FetchEntityCollection(myCustomer.Orders, myCustomer.GetRelationInfoOrders()) End Using
// C# EntityCollection<CustomerEntity> customers = new EntityCollection<CustomerEntity>(); PrefetchPath2 path = new PrefetchPath2(EntityType.CustomerEntity); path.Add(CustomerEntity.PrefetchPathOrders).SubPath.Add(OrderEntity.PrefetchPathEmployees); using(DataAccessAdapter adapter = new DataAccessAdapter()) { adapter.FetchEntityCollection(customers, null, path); } // setup projections per type. List<IEntityPropertyProjector> customerProjections = EntityFields2.ConvertToProjectors( EntityFieldsFactory.CreateEntityFieldsObject(EntityType.CustomerEntity)); // add an additional projector so the destination DataTable will have an additional column called 'IsNew' with // the value of the IsNew property of the customer entities. customerProjections.Add(new EntityPropertyProjector(new EntityProperty("IsNew"), "IsNew")); List<IEntityPropertyProjector> orderProjections = EntityFields2.ConvertToProjectors( EntityFieldsFactory.CreateEntityFieldsObject(EntityType2.OrderEntity)); List<IEntityPropertyProjector> employeeProjections = EntityFields.ConvertToProjectors( EntityFieldsFactory.CreateEntityFieldsObject(EntityType2.EmployeeEntity)); List<IViewProjectionData> projectionData = new List<IViewProjectionData>(); // create the customer projection information. Specify a filter so only customers from Germany // are projected. projectionData.Add(new ViewProjectionData<CustomerEntity>( customerProjections, (CustomerFields.Country == "Germany"), true)); projectionData.Add(new ViewProjectionData<OrderEntity>(orderProjections, null, false)); projectionData.Add(new ViewProjectionData<EmployeeEntity>(employeeProjections)); DataSet result = new DataSet("projectionResult"); customers.CreateHierarchicalProjection(projectionData, result);
' VB.NET Dim customers As New EntityCollection(Of CustomerEntity)() Dim path As New PrefetchPath2(EntityType.CustomerEntity) path.Add(CustomerEntity.PrefetchPathOrders).SubPath.Add(OrderEntity.PrefetchPathEmployees) Using adapter As New DataAccessAdapter() adapter.FetchEntityCollection(customers, Nothing, path) End Using ' setup projections per type. Dim customerProjections As List(Of IEntityPropertyProjector) = EntityFields2.ConvertToProjectors( _ EntityFieldsFactory.CreateEntityFieldsObject(EntityType.CustomerEntity)) ' add an additional projector so the destination DataTable will have an additional column called 'IsNew' with ' the value of the IsNew property of the customer entities. customerProjections.Add(New EntityPropertyProjector(New EntityProperty("IsNew"), "IsNew")) Dim orderProjections As List(Of IEntityPropertyProjector) = EntityFields2.ConvertToProjectors( _ EntityFieldsFactory.CreateEntityFieldsObject(EntityType.OrderEntity)) Dim employeeProjections As List(Of IEntityPropertyProjector) = EntityFields2.ConvertToProjectors( _ EntityFieldsFactory.CreateEntityFieldsObject(EntityType.EmployeeEntity)) Dim projectionData As New List(Of IViewProjectionData)() ' create the customer projection information. Specify a filter so only customers from Germany ' are projected. projectionData.Add(New ViewProjectionData(Of CustomerEntity)( _ customerProjections, (CustomerFields.Country = "Germany"), True)) projectionData.Add(New ViewProjectionData(Of OrderEntity)(orderProjections, Nothing, False)) projectionData.Add(New ViewProjectionData(Of EmployeeEntity)(employeeProjections)) Dim result As New DataSet("projectionResult") customers.CreateHierarchicalProjection(projectionData, result)
// C# EntityCollection<CustomerEntity> customers = new EntityCollection<CustomerEntity>(); PrefetchPath2 path = new PrefetchPath2(EntityType.CustomerEntity); path.Add(CustomerEntity.PrefetchPathOrders).SubPath.Add(OrderEntity.PrefetchPathEmployees); using(DataAccessAdapter adapter = new DataAccessAdapter()) { adapter.FetchEntityCollection(customers, null, path); } // setup projections per type. List<IEntityPropertyProjector> customerProjections = EntityFields2.ConvertToProjectors( EntityFieldsFactory.CreateEntityFieldsObject(EntityType.CustomerEntity)); // add an additional projector so the destination DataTable will have an additional column called 'IsNew' with // the value of the IsNew property of the customer entities. customerProjections.Add(new EntityPropertyProjector(new EntityProperty("IsNew"), "IsNew")); List<IEntityPropertyProjector> orderProjections = EntityFields2.ConvertToProjectors( EntityFieldsFactory.CreateEntityFieldsObject(EntityType2.OrderEntity)); List<IEntityPropertyProjector> employeeProjections = EntityFields.ConvertToProjectors( EntityFieldsFactory.CreateEntityFieldsObject(EntityType2.EmployeeEntity)); List<IViewProjectionData> projectionData = new List<IViewProjectionData>(); // create the customer projection information. Specify a filter so only customers from Germany // are projected. projectionData.Add(new ViewProjectionData<CustomerEntity>( customerProjections, (CustomerFields.Country == "Germany"), true)); projectionData.Add(new ViewProjectionData<OrderEntity>(orderProjections, null, false)); projectionData.Add(new ViewProjectionData<EmployeeEntity>(employeeProjections)); Dictionary<Type, IEntityCollection> projectionResults = new Dictionary<Type, IEntityCollection>(); customers.CreateHierarchicalProjection(projectionData, projectionResults);
' VB.NET Dim customers As New EntityCollection(Of CustomerEntity)() Dim path As New PrefetchPath2(EntityType.CustomerEntity) path.Add(CustomerEntity.PrefetchPathOrders).SubPath.Add(OrderEntity.PrefetchPathEmployees) Using adapter As New DataAccessAdapter() adapter.FetchEntityCollection(customers, Nothing, path) End Using ' setup projections per type. Dim customerProjections As List(Of IEntityPropertyProjector) = EntityFields2.ConvertToProjectors( _ EntityFieldsFactory.CreateEntityFieldsObject(EntityType.CustomerEntity)) ' add an additional projector so the destination DataTable will have an additional column called 'IsNew' with ' the value of the IsNew property of the customer entities. customerProjections.Add(New EntityPropertyProjector(New EntityProperty("IsNew"), "IsNew")) Dim orderProjections As List(Of IEntityPropertyProjector) = EntityFields2.ConvertToProjectors( _ EntityFieldsFactory.CreateEntityFieldsObject(EntityType.OrderEntity)) Dim employeeProjections As List(Of IEntityPropertyProjector) = EntityFields2.ConvertToProjectors( _ EntityFieldsFactory.CreateEntityFieldsObject(EntityType.EmployeeEntity)) Dim projectionData As New List(Of IViewProjectionData)() ' create the customer projection information. Specify a filter so only customers from Germany ' are projected. projectionData.Add(New ViewProjectionData(Of CustomerEntity)( _ customerProjections, (CustomerFields.Country = "Germany"), True)) projectionData.Add(New ViewProjectionData(Of OrderEntity)(orderProjections, Nothing, False)) projectionData.Add(New ViewProjectionData(Of EmployeeEntity)(employeeProjections)) Dim projectionResults As New Dictionary(Of Type, IEntityCollection)() customers.CreateHierarchicalProjection(projectionData, projectionResults)
Note: |
If you just want a structure with per entity type a collection with all the instances of that type in the entity graph, please use the routine ObjectGraphUtils.ProduceCollectionsPerTypeFromGraph. The ObjectGraphUtils class is located in the ORMSupportClasses namespace and contains a variety of routines working on entity graphs. Please see the LLBLGen Pro reference manual for details on this class and this method. |
// C# // First fetch all customers from Germany with their orders. EntityCollection<CustomerEntity> customers = new EntityCollection<CustomerEntity>(); PrefetchPath2 path = new PrefetchPath2(EntityType.CustomerEntity); path.Add(CustomerEntity.PrefetchPathOrders); using(DataAccessAdapter adapter = new DataAccessAdapter()) { adapter.FetchEntityCollection(customers, new RelationPredicateBucket(CustomerFields.Country == "Germany"), path); } // we now will add a tracker collection to the orders collection of customer 0. EntityCollection<OrderEntity> tracker = new EntityCollection<OrderEntity>(); customers[0].Orders.RemovedEntitiesTracker = tracker; // after this, we can do this: customers[0].Orders.Remove(myOrder); // and myOrder is removed from the in-memory collection customers[0].Orders // and it's placed in 'tracker'. We can now delete the entities in tracker // by using a UnitOfWork2 object or by calling adapter.DeleteEntityCollection(tracker).
' VB.NET ' First fetch all customers from Germany with their orders. Dim customers As New EntityCollection(Of CustomerEntity)() Dim path As New PrefetchPath2(EntityType.CustomerEntity) path.Add(CustomerEntity.PrefetchPathOrders) Using adapter As New DataAccessAdapter() adapter.FetchEntityCollection(customers, _ new RelationPredicateBucket(CustomerFields.Country = "Germany"), _ path) End Using ' we now will add a tracker collection to the orders collection of customer 0. Dim tracker As New EntityCollection(Of OrderEntity)() customers(0).Orders.RemovedEntitiesTracker = tracker ' after this, we can do this: customers(0).Orders.Remove(myOrder) ' and myOrder is removed from the in-memory collection customers[0].Orders ' and it's placed in 'tracker'. We can now delete the entities in tracker ' by using a UnitOfWork2 object or by calling adapter.DeleteEntityCollection(tracker).
Note: |
Tracking removal of an entity isn't used by the Clear() method, because Clear is often used to clean up a collection and not to remove entities from the database, so to avoid false positives and the deletion of entities which weren't suppose to be deleted, removal tracking isn't available for the Clear method. |