none
How to cast result from EntityDataReader to an Entity object RRS feed

  • Question

  • Hi

    I execute an ESQL query using an EntityCommand and run it via cmd.ExecuteReader(). How do I cast the result from the EntityDataReader to an Entity object, in my case a Customer object ?

    I can get the customerID using drd.GetString(0), but I'd like to have the whole result as an Entity object.

    Using conn As New EntityConnection("name=NorthwindEntities")
    	Dim strESQL As String = "SELECT VALUE cust FROM NorthwindEntities.Customers as cust WHERE cust.CustomerID = 'ALFKI'"
    	conn.Open()
    	Dim cmd As EntityCommand = conn.CreateCommand()
    	cmd.CommandType = CommandType.Text
    	cmd.CommandText = strESQL
    	Using drd As EntityDataReader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)
    		While drd.Read()
    			Dim custID As String = drd.GetString(0)
    		End While
    	End Using

     

    Thank you in advance for any help

    • Edited by AlbertWa Thursday, January 12, 2012 8:20 AM
    Thursday, January 12, 2012 8:17 AM

Answers

  • On 1/12/2012 6:59 AM, AlbertWa wrote:
    > Hi darnold924
    >
    > Thank you for your answer. I'm not sure I understand your answer. Maybe
    > my explanation was not so clear. What I'd like to do is in the loop
    > "While drd.Read()" be able to cast the row (DdDataRecord) as a Customer
    > object. I would like something like :
    >
    >             Dim  aList  As  New  List(Of  Customer)
    >             Using  drd  As  EntityDataReader  =  cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)
    >                 While  drd.Read()
    >                     Dim  cust  As  Customer  =  drd.GetObject()  '  or  a  something  similar
    >                     aList.Add(cust)
    >                 End  While
    >             End  Using
    >
    > Of course I could get the value from all fields using drd("fieldname")
    > and create from these values a customer object. But is there no direct
    > way, as the drd object contains all fields values ?
    >
    > Thank you for your help
    >
     
    No, there is no direct way you can tell the datareader to populate the
    object that I know of, just like you cannot tell an ADO.NET datareader
    to populate a custom object you built to hold the data from a returned
    resultset using ADO.NET and SQL Command objects with some kind of direct
    way of population of the object from the datareader.
     However, you can use Entity SQL this way too.
     
     
    • Marked as answer by AlbertWa Thursday, January 12, 2012 1:55 PM
    Thursday, January 12, 2012 1:50 PM

All replies

  • On 1/12/2012 3:17 AM, AlbertWa wrote:
    > Hi
    >
    > I execute an ESQL query using an EntityCommand and run it via
    > cmd.ExecuteReader(). How do I cast the result from the EntityDataReader
    > to an Entity object, in my case a Customer object ?
    >
    > I can get the customerID using drd.GetString(0), but I'd like to have
    > the whole result as an Entity object.
     
    You use the entity on the model. You can instantiate an entity on the
    model, an object,  and populate the object. You have to materialize the
    object on the model and you populate it. An entity is an object.
     
    >
    > Using  conn  As  New  EntityConnection("name=NorthwindEntities")
    >     Dim  strESQL  As  String  =  "SELECT  VALUE  cust  FROM  NorthwindEntities.Customers  as  cust WHERE  cust.CustomerID  =  'ALFKI'"
    >     conn.Open()
    >     Dim  cmd  As  EntityCommand  =  conn.CreateCommand()
    >     cmd.CommandType  =  CommandType.Text
    >     cmd.CommandText  =  strESQL
    >     Using  drd  As  EntityDataReader  =  cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)
    >         While  drd.Read()
                     Dim NorthwindEntities.Customers cust = new
    NorthwindEntities.Customers
                     cust.ID = Convert.ToInt32(drd["CustID"])
     
    >             Dim  custID  As  String  =  drd.GetString(0)
                             '' you don't need to use the ordinal
    drd.GetString(0) you and use drd["CustID"] to address a column in a row
    in the returned results by its column name.
     
    >         End  While
    >     End  Using
    >
    > Thank you in advance for any help
    >
     
    Now, if you had to return a Customer from a called method, you you would
    do the same thing as above
     
    public GetCustomerbyID(id As Int32) AS  NorthwindEntities.Customers
     
    You make a new Customer.
    You populate it.
    You return it.
     
    end sub
     If you have to return a List<T> of Customers, you do this
     
    public GetCustomers() AS  ListOf(NorthwindEntities.Customers)
     
    dim  a new List<T> a ListOf(NorthwindEntities.Customers)  -- call it
    customers.
     
    You query
     
    in the reader loop
     
    You make a new Customer.
    You populate the object.
    You load Customer into the List<T>
     
    customer.Add(customer)
     
    You return the List of Customers.
     
    end sub
     
    Thursday, January 12, 2012 11:22 AM
  • Hi darnold924

    Thank you for your answer. I'm not sure I understand your answer. Maybe my explanation was not so clear. What I'd like to do is in the loop "While drd.Read()" be able to cast the row (DdDataRecord) as a Customer object. I would like something like :

    			Dim aList As New List(Of Customer)
    			Using drd As EntityDataReader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)
    				While drd.Read()
    					Dim cust As Customer = drd.GetObject() ' or a something similar
    					aList.Add(cust)
    				End While
    			End Using
    

    Of course I could get the value from all fields using drd("fieldname") and create from these values a customer object. But is there no direct way, as the drd object contains all fields values ?

    Thank you for your help

    Thursday, January 12, 2012 11:59 AM
  • On 1/12/2012 6:59 AM, AlbertWa wrote:
    > Hi darnold924
    >
    > Thank you for your answer. I'm not sure I understand your answer. Maybe
    > my explanation was not so clear. What I'd like to do is in the loop
    > "While drd.Read()" be able to cast the row (DdDataRecord) as a Customer
    > object. I would like something like :
    >
    >             Dim  aList  As  New  List(Of  Customer)
    >             Using  drd  As  EntityDataReader  =  cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)
    >                 While  drd.Read()
    >                     Dim  cust  As  Customer  =  drd.GetObject()  '  or  a  something  similar
    >                     aList.Add(cust)
    >                 End  While
    >             End  Using
    >
    > Of course I could get the value from all fields using drd("fieldname")
    > and create from these values a customer object. But is there no direct
    > way, as the drd object contains all fields values ?
    >
    > Thank you for your help
    >
     
    No, there is no direct way you can tell the datareader to populate the
    object that I know of, just like you cannot tell an ADO.NET datareader
    to populate a custom object you built to hold the data from a returned
    resultset using ADO.NET and SQL Command objects with some kind of direct
    way of population of the object from the datareader.
     However, you can use Entity SQL this way too.
     
     
    • Marked as answer by AlbertWa Thursday, January 12, 2012 1:55 PM
    Thursday, January 12, 2012 1:50 PM