Microsoft Developer Network > Página principal de foros > Windows Presentation Foundation (WPF) > Unable to select specific fields in an EF query in VS2010 Beta 1
Formular una preguntaFormular una pregunta
 

RespondidaUnable to select specific fields in an EF query in VS2010 Beta 1

  • sábado, 22 de agosto de 2009 16:38Craigton Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Hi,

    I've been following Beth Massis' excellent videos on Entity Framework and can successfully do a query on the OMS Customer table and populate a datagrid in VS2010 Beta 1. However, when I try to be selective in the fields I retrieve I hit a problem. I've looked at MSDN library where it shows the following example, using the Adventure works database advWorksContext.Product.Select("it.ProductID, it.Name") as the menas by which which specific fields can be retrieved. All of the samples show it.Field Name but I have no idea whether the requirement is to use "it" or the actual Table Name so I tried both!

    This code is entered by VS2010 when one drags from the Customers Data Source returns the whole of the Customers table 
    Dim CustomersQuery As System.Data.Objects.ObjectQuery(Of TestEF.Customer) = OMSEntities.Customers
    Return CustomersQuery

    But when I try to select only FirstName and LastName thus

    Dim CustomersQuery As System.Data.Objects.ObjectQuery(Of TestEF.Customer) = OMSEntities.Customers.Select("it.LastName, it.FirstName")
    or
    Dim CustomersQuery01 As System.Data.Objects.ObjectQuery(Of TestEF.Customer) = OMSEntities.Customers.Select("Customers.LastName, Customers.FirstName")

    I get the following error:

    Value of type 'System.Data.Objects.ObjectQuery(Of System.Data.Common.DbDataRecord)' cannot be converted to 'System.Data.Objects.ObjectQuery(Of TestEF.Customer)'.

     

    I iamagine I'm missing something obvious but would appreciate any clues!


    Thanks in advance


Respuestas

  • viernes, 28 de agosto de 2009 10:18Craigton Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Hi Jim,

    Not sure what you want. As I said previously, I created the project, added an EF item, generated from the database, the system creates the datasources and I simply dragged the grid onto the dssign surface and code behind was automatically to select every field in the Customer table. When I try to limit the number of customer fields to be selected the error is generated, i.e.  

    This code is entered by VS2010 when one drags from the Customers Data Source returns the whole of the Customers table 
    Dim CustomersQuery As System.Data.Objects.ObjectQuery(Of TestEF.Customer) = OMSEntities.Customers
    Return CustomersQuery

    But when I try to select only FirstName and LastName thus
    Dim CustomersQuery As System.Data.Objects.ObjectQuery(Of TestEF.Customer) = OMSEntities.Customers.Select("it.LastName, it.FirstName")
    or
    Dim CustomersQuery01 As System.Data.Objects.ObjectQuery(Of TestEF.Customer) = OMSEntities.Customers.Select("Customers.LastName, Customers.FirstName")
    I get the following error:

    Value of type 'System.Data.Objects.ObjectQuery(Of System.Data.Common.DbDataRecord)' cannot be converted to 'System.Data.Objects.ObjectQuery(Of TestEF.Customer)'.


    Regards
    • Marcado como respuestaCraigton miércoles, 04 de noviembre de 2009 18:34
    •  
  • miércoles, 04 de noviembre de 2009 18:33Craigton Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Just closing this off.
    • Marcado como respuestaCraigton miércoles, 04 de noviembre de 2009 18:34
    •  

Todas las respuestas

  • lunes, 24 de agosto de 2009 11:25Jim Zhou - MSFTModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    Hi,

    --> All of the samples show it.Field Name but I have no idea whether the requirement is to use "it" or the actual Table Name so I tried both!
    Did you encounter the issue when using Designer in Visual Studio 2010? or the issue is mainly related to DataGrid control? if so, you can post it to WPF Discussion forum for more information.
    WPF Discussion: http://www.codeplex.com/wpf/Thread/List.aspx
    -->drags from the Customers Data Source
    How did you define the Customers data source?

    Thanks.


    Jim Zhou -MSFT
  • lunes, 24 de agosto de 2009 12:49Craigton Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Hi,

    Thee's no code from me whatsoever. I created a WPF project, added the EF new item for the OMS database and the system creates the appropriate data sources from the OMS databse for each of the 4 tables. I then used the designer simply to drag the Customers table from the Data Sources window onto the design surface. So all data sources are created by VS2010. Similarly, the VB code is automatically generated and works fine to populate the grid. 

    It's when I try to limit the selection to a couple of specific fields that gives grief so it does not seem to be an issue with the datagrid, but rather the ObjectQuery.

    Thanks
  • viernes, 28 de agosto de 2009 9:09Jim Zhou - MSFTModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Hi,
    -->It's when I try to limit the selection to a couple of specific fields that gives grief so it does not seem to be an issue with the datagrid, but rather the ObjectQuery.
    It seems that you assigned the data source to the controls by designer manually, right?But i am not sure how you perform the "limit the selection"? Could you be more specific about your scenario? A detailed demonstration will be better.

    Thanks.

    Jim Zhou -MSFT
  • viernes, 28 de agosto de 2009 10:18Craigton Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Hi Jim,

    Not sure what you want. As I said previously, I created the project, added an EF item, generated from the database, the system creates the datasources and I simply dragged the grid onto the dssign surface and code behind was automatically to select every field in the Customer table. When I try to limit the number of customer fields to be selected the error is generated, i.e.  

    This code is entered by VS2010 when one drags from the Customers Data Source returns the whole of the Customers table 
    Dim CustomersQuery As System.Data.Objects.ObjectQuery(Of TestEF.Customer) = OMSEntities.Customers
    Return CustomersQuery

    But when I try to select only FirstName and LastName thus
    Dim CustomersQuery As System.Data.Objects.ObjectQuery(Of TestEF.Customer) = OMSEntities.Customers.Select("it.LastName, it.FirstName")
    or
    Dim CustomersQuery01 As System.Data.Objects.ObjectQuery(Of TestEF.Customer) = OMSEntities.Customers.Select("Customers.LastName, Customers.FirstName")
    I get the following error:

    Value of type 'System.Data.Objects.ObjectQuery(Of System.Data.Common.DbDataRecord)' cannot be converted to 'System.Data.Objects.ObjectQuery(Of TestEF.Customer)'.


    Regards
    • Marcado como respuestaCraigton miércoles, 04 de noviembre de 2009 18:34
    •  
  • miércoles, 04 de noviembre de 2009 18:33Craigton Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Just closing this off.
    • Marcado como respuestaCraigton miércoles, 04 de noviembre de 2009 18:34
    •