Generic Query Builder

Posts   
 
    
nw
User
Posts: 42
Joined: 09-Jun-2010
# Posted on: 15-Jun-2010 09:28:44   

Hi,

Is it possible to build a generic query? Can you please give me an example?

For example, if I have a list of entities and fields that I want to display, but they are all a list of strings such as

Entity: "Customer" Fields: "Name", "Address"

From what I have read so far, I can use reflection to construct the fields and add them to ResultsetFields. Is that the only way?

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 15-Jun-2010 09:41:00   

No other built-in way for that.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 15-Jun-2010 09:59:08   

You can obtain Field instances from an entity by using the string (name of the field) as indexer for the 'Fields' property of an entity.

Then use a projection in memory (see projection docs in the manual for details) to create a projection which reflects the fields you want, e.g. in a datatable.

Frans Bouma | Lead developer LLBLGen Pro
nw
User
Posts: 42
Joined: 09-Jun-2010
# Posted on: 15-Jun-2010 10:01:18   

So am I correct to say that the only way to build a LLBLGen query is by defining fields using ResultsetFields and executing the query using DataAccessAdapter?

Can you point me to any documentations for building a query?

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 15-Jun-2010 10:27:01   

Using the entityName as a string, you can get an instance of the entity as follows:

var entityType = (EntityType)Enum.Parse(typeof(EntityType), entityName, false);
var myEntity = GeneralEntityFactory.Create(entityType);

Having an instance of the Entity, you can use the following code to get an EntityField

var entityField1 = myEntity.Fields["Address"];

Then you can build the DynaicList ResultsetFields as follows

var fields = new ResultsetFields(n);
fields.DefineField(entityField1, 0);
...

And you can fetch it as follows:

DataAccessAdapter adapter = new DataAccessAdapter();
DataTable dynamicList = new DataTable();
adapter.FetchTypedList(fields, dynamicList, ...);
nw
User
Posts: 42
Joined: 09-Jun-2010
# Posted on: 15-Jun-2010 13:00:44   

Thanks for the detailed explanations.