Convert DataReader to Entity list

Convert DataReader to Entity list

Old forum URL: forums.lhotka.net/forums/t/2328.aspx


kobruleht posted on Monday, February 12, 2007

I need to convert datareader to strongly typed entity list.
The following procedure returns error.
How to fix this ?

List<xEntity> CreateEntityList<xEntity>(IDataReader dataReader) {

List<xEntity> list = new List<xEntity>();
while (dataReader.Read()) {
 object[] o;
 dataReader.GetValues(o);
 // the following line causes errors:
 //Error 1 The best overloaded method match for
 // 'System.Collections.Generic.List<xEntity>.Add(xEntity)' has
 //some invalid arguments 
 //Error 2 Argument '1': cannot convert from 'object[]' to 'xEntity'
 list.Add(o);
 }
dataReader.Close();
return list;
}

skagen00 replied on Monday, February 12, 2007

I'm not sure what you are expecting...

You're getting an array of object with GetValues. The generic list collectionly has one Add(), and that's to take an instance of whatever your generic is.

It's expecting that you add an xEntity - not an array of object values.

If you wanted to have a factory method on xEntity such as:

public static xEntity NewxEntity(object[] params)

{

   // Build and return xEntity instance

}

Then you could do:

List<xEntity>.Add(xEntity.NewxEntity(o));

But typically you wouldn't be using generic list collections with CSLA (I occasionally use them for certain purposes but not for main entities).

Perhaps a little more context to your question would help.

 

Good luck,

Chris

 

kobruleht replied on Wednesday, February 14, 2007

I'm sorry for unclear question.

I have lot of entity classes like

class CustomerEntity {

  public string Name {

   get { return  Row["Name"] ;}

   set { Row["Name"]=value; }

  }

DataRow Row;

}

I have BO classes for every entity class. In business object class data retrieval method I want to convert datareader to entity list  like :

public List<CustomerEntity> GetCustomerList() {

IDataReader dr;

dr = .... code to create datareader

return CreateEntityList<CustomerEntity>(dr);

}

I need to create generic CreateEntityList method using probably reflection.  Can I use DataMapper class for this ?

 

Copyright (c) Marimer LLC