LLBLGenProDataSource2 to DataView

Posts   
 
    
pvilanova
User
Posts: 24
Joined: 01-Apr-2007
# Posted on: 25-Sep-2008 07:24:34   

Hi folks:

There is some way to get a DataView from a LLBLGenProDataSource2?

I need that to export the Datasource data to a excel sheet.

Regards and thank you very much.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Sep-2008 08:23:46   

Hi there. Try this:

// first, create a view based on the actual fetched collection on the LLBLGenProDataSource
IEntityView2 view = _llblgenProDS.EntityCollection.DefaultView;

// create the propertyProjectors.
List<IEntityPropertyProjector> propertyProjectors = new List<IEntityPropertyProjector>();
foreach (EntityField2 field in _llblgenProDS.EntityCollection.EntityFactoryToUse.CreateFields())
{
    propertyProjectors.Add(new EntityPropertyProjector(field, field.Name));
}

// the dataTable to fill
DataTable projectionResults = new DataTable();

// create the actual projection.
view.CreateProjection(propertyProjectors, projectionResults);
David Elizondo | LLBLGen Support Team
pvilanova
User
Posts: 24
Joined: 01-Apr-2007
# Posted on: 26-Sep-2008 04:39:36   

Daelmo:

Thanks for the quick response.

That solution does not include prefetch paths. I need to get exactly the same contents of the datasource and not only the data from entities.

Thank you very much. Regards.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 26-Sep-2008 10:16:25   

Please check the example of Hierarchical projections .

pvilanova
User
Posts: 24
Joined: 01-Apr-2007
# Posted on: 26-Sep-2008 22:13:28   

Walaa:

I've checked the example but i was not able to project any prefetch path at all.

The problem with this code,

        foreach (IPrefetchPathElement2 pre in _InstrumentDS.PrefetchPathToUse)
            foreach (EntityField2 fie in pre.EntityFactoryToUse.CreateFields())
                propertyProjectors.Add(new EntityPropertyProjector(fie, fie.Name));

is pre.EntityFactoryToUse == null

Any ideas?

Thank you very much. Regards.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 27-Sep-2008 14:50:50   

If you set the datasource to some prefetch path, you can use that same prefetch path to fetch the projection.

Frans Bouma | Lead developer LLBLGen Pro
pvilanova
User
Posts: 24
Joined: 01-Apr-2007
# Posted on: 27-Sep-2008 18:32:09   

Ok, the problem is:

I cannot get the prefetchpath fields, bcoz the EntityFactoryToUse is null.

Can you please post example code?

Regards.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Sep-2008 09:09:46   

pvilanova wrote:

Walaa:

I've checked the example but i was not able to project any prefetch path at all.

The problem with this code,

        foreach (IPrefetchPathElement2 pre in _InstrumentDS.PrefetchPathToUse)
            foreach (EntityField2 fie in pre.EntityFactoryToUse.CreateFields())
                propertyProjectors.Add(new EntityPropertyProjector(fie, fie.Name));

is pre.EntityFactoryToUse == null

Any ideas?

Thank you very much. Regards.

Are you sure you tried the Hierarchical Projection example? coz that indeed should work:

// -- the projectionData to fill
List<IViewProjectionData> projectionData = new List<IViewProjectionData>();


// -- setup projections per type.

// customers (the root type)
List<IEntityPropertyProjector> customerProjections = EntityFields2.ConvertToProjectors(
        EntityFieldsFactory.CreateEntityFieldsObject(EntityType.CustomersEntity));
projectionData.Add(new ViewProjectionData<CustomersEntity>(customerProjections, null, true));

// orders
List<IEntityPropertyProjector> orderProjections = EntityFields2.ConvertToProjectors(
        EntityFieldsFactory.CreateEntityFieldsObject(EntityType.OrdersEntity));
projectionData.Add(new ViewProjectionData<OrdersEntity>(orderProjections, null, false));

// employee
List<IEntityPropertyProjector> employeeProjections = EntityFields2.ConvertToProjectors(
        EntityFieldsFactory.CreateEntityFieldsObject(EntityType.OrderDetailsEntity));
projectionData.Add(new ViewProjectionData<OrderDetailsEntity>(employeeProjections));


// -- get the DataSet results (here, _customerDS is a LLBLGenProDataSource2                     
DataSet result = new DataSet("projectionResult");
_customersDS.EntityCollection.CreateHierarchicalProjection(projectionData, result);

That assumes:

  • The data is fetched and ready to use in your LLBLGenProDataSource2
  • You know the prefetched graph and what types of that graph you want to project to the result DataSet.

Also, as you want a Hierarchical projection (root entity + prefetchtPaths) the result should be a DataSet, not a DataView simple_smile

Hope helpful wink

David Elizondo | LLBLGen Support Team
luciusism
User
Posts: 119
Joined: 02-Jun-2007
# Posted on: 01-Dec-2008 09:55:53   

To follow-up on what pvilanova wrote, is there a recommended way of converting prefetched entities w/in a collection into a projected dataTable automatically? In otherwords, I'd like to pass an entity collection into a method and have it and all prefetched entities converted into a dataTabe.

The above recommendation would seem to only work where I know beforehand what entities are prefetched.

Thanks!

pvilanova wrote: Walaa:

I've checked the example but i was not able to project any prefetch path at all.

The problem with this code, Code: foreach (IPrefetchPathElement2 pre in _InstrumentDS.PrefetchPathToUse) foreach (EntityField2 fie in pre.EntityFactoryToUse.CreateFields()) propertyProjectors.Add(new EntityPropertyProjector(fie, fie.Name));

is pre.EntityFactoryToUse == null

Any ideas?

Thank you very much. Regards.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 01-Dec-2008 11:03:45   

The above recommendation would seem to only work where I know beforehand what entities are prefetched.

If you don't know the prefetched entities beforehand.

Then you'd have to do it manually: 1- Looping on the passed in collection and calling each entity's GetDependingRelatedEntities() & GetDependentRelatedEntities() methods, to get the attached related entities.

Please check the reference manual for these methods.

2- then you'll have to use the EntityField collection (entity.Fields) to loop on fields and start copying data into datatables.

luciusism
User
Posts: 119
Joined: 02-Jun-2007
# Posted on: 01-Dec-2008 23:23:44   

Hi Walaa, thank you for the reply. The suggestion you provide is very useful. However, I was wondering if it would be more efficient to pass to my function a hint as to which prefetched entities to project into the dataTable. For example, if I were to pass the same prefetchPath2 collection as used to fetch the entityCollection in the first place. This is why I was interested in the earlier poster's suggestion about iterating through the prefetch elements.

Is there a way to get the fields from prefetch entities based on the prefetchpath passed? The earlier suggestion of prefetchPAthElement.EntityFactoryToUse.CreateFields() does not seem to work since the entitfactoryToUse is null.

Thanks!

luciusism
User
Posts: 119
Joined: 02-Jun-2007
# Posted on: 01-Dec-2008 23:28:19   

this the following function give an idea of what I'm trying to achieve:


public DataTable CollectionToDataTable<TEntity>(EntityCollection<TEntity> collection, IPrefetchPath2 pp) where TEntity : EntityBase2, IEntity2
{
    // fields for the base element
    List<IEntityPropertyProjector> propertyProjectors = EntityFields2.ConvertToProjectors(collection.EntityFactoryToUse.CreateFields());
    
    // fields for each prefetched element
    if (pp != null && pp.Count != 0)
    {
        for (int i = 0; i < pp.Count; i++)
        {
            // ERROR! this is where the error occurs, since EntityFactoryToUse is always null for prefetch paths
            // questio is how to get the entity fields from these prefetched entities
            foreach (IEntityField2 f in pp[i].EntityFactoryToUse.CreateFields())
            {
                propertyProjectors.Add(new EntityPropertyProjector(f, pp[i].EntityFactoryToUse.ForEntityName + f.Name));
            }
        }
    }
    DataTable dt = new DataTable();
    collection.DefaultView.CreateProjection(propertyProjectors, dt);
    return dt;
}

Note int he above comments where the error occurs in converting prefetched entities into fields collections for projection

luciusism
User
Posts: 119
Joined: 02-Jun-2007
# Posted on: 02-Dec-2008 00:02:16   

please ignore the previous post, I now realize that the collection.DefaultView.CreateProjection(propertyProjectors, dt) function will not pull data from the prefetched entities, only the base entity from the entityCollection. Back to the drawing board simple_smile