Create datasource2 based on string name of Entity type

Posts   
 
    
Posts: 4
Joined: 11-Aug-2017
# Posted on: 11-Aug-2017 17:56:01   

I'm using v 4.2 final.

I'm doing this:

Using adapter As New DataAccessAdapter
  Dim metaData As New LinqMetaData(adapter)

  dim s = metaData.JtCity.Select(function(c) c.ColumnCommonToAllTables)

End Using

I'd like to retrieve the datasource2 dynamically based on the string name of the entity.

Something like this:

Using adapter As New DataAccessAdapter

  Dim metaData As New LinqMetaData(adapter)

  dim ds2 = GetDataSource2("JtCityEntity", adapter, metaData)

  dim s = ds2.Select(function(c) c.ColumnCommonToAllTables)

End Using

From what I've read, I think I might need to use Activator.CreateInstance but I'm not sure how to get started with that to create a datasource2 object.

Thanks!

Posts: 4
Joined: 11-Aug-2017
# Posted on: 11-Aug-2017 20:46:23   

I got a bit further...

Was able to dynamically create the datasource2(of JtCityEntity) but it's not queryable. Any ideas? Thanks!


Using adapter As New DataAccessAdapter

            Dim metadata As New LinqMetaData(adapter)

            Dim propertyInfoJtCity = metadata.GetType().GetProperties().FirstOrDefault(Function(p) p.Name = "JtCity")

            Dim dataSource2OfJtCity = propertyInfoJtCity.GetValue(metadata, Nothing)

            ' How to make dataSource2OfJtCity queryable?
            ' to allow me to do this

            Dim cities = dataSource2OfJtCity.ToList().Select(Function(c) c.Fields.FirstOrDefault(Function(f) f.Name = "CityName")).ToList()

            Dim x = 0

        End Using


Posts: 4
Joined: 11-Aug-2017
# Posted on: 11-Aug-2017 21:25:30   

This worked:

TryCast(propertyInfoJtCity.GetValue(metadata, Nothing), IQueryable(Of IEntity2))

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 12-Aug-2017 10:10:56   

Based on string name is doable: use

Enum.Parse(GetType(EntityType), "MyEntity")

and cast it to int. Then you can use LinqMetaData.GetQueryableForEntity(intValue) to obtain an IDataSource instance for an entity

Frans Bouma | Lead developer LLBLGen Pro
Posts: 4
Joined: 11-Aug-2017
# Posted on: 16-Aug-2017 06:39:28   

Thank You Otis.

I had gone that route initially but didn't know how to use the IDatasource to query the data using the LinqMetaData. In other words the IDatasource was not queryable if I remember correctly.

I did get this all working with this code:


Using adapter As New DataAccessAdapter

            Dim metaData As New LinqMetaData(adapter)
            Dim entType = EntityType.ToString().Replace("Entity", "")
            Dim entityPropertyInfo = metaData.GetType().GetProperties().FirstOrDefault(Function(p) p.Name = entType)
            Dim dataSource2OfEntity2 = TryCast(entityPropertyInfo.GetValue(metaData, Nothing), IQueryable(Of IEntity2))

            listItems = dataSource2OfEntity2.ToList() _
                        .Select(Function(e) New JtListItemsEntity With
                            {.ItemId = e.Fields.FirstOrDefault(Function(f) f.Name = TableIdFieldname).CurrentValue,
                             .ItemText = e.Fields.FirstOrDefault(Function(f) f.Name = TableDisplayMemberFieldName).CurrentValue,
                             .ItemValue = e.Fields.FirstOrDefault(Function(f) f.Name = TableValueMemberFieldName).CurrentValue,
                             .RecordDeleted = e.Fields.FirstOrDefault(Function(f) f.Name = JtListItemsFields.RecordDeleted.Name).CurrentValue,
                             .JtListCategories = New JtListCategoriesEntity() With {.Category = ListCategoryName}}) _
                        .ToList()

            adapter.CloseConnection()
        End Using

Thanks again!