Microsoft Developer Network > Forenhomepage > Windows Presentation Foundation (WPF) > Unable to select specific fields in an EF query in VS2010 Beta 1
Stellen Sie eine FrageStellen Sie eine Frage
 

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

  • Samstag, 22. August 2009 16:38Craigton TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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


Antworten

  • Freitag, 28. August 2009 10:18Craigton TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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
    • Als Antwort markiertCraigton Mittwoch, 4. November 2009 18:34
    •  
  • Mittwoch, 4. November 2009 18:33Craigton TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    Just closing this off.
    • Als Antwort markiertCraigton Mittwoch, 4. November 2009 18:34
    •  

Alle Antworten

  • Montag, 24. August 2009 11:25Jim Zhou - MSFTModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     

    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
  • Montag, 24. August 2009 12:49Craigton TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Freitag, 28. August 2009 09:09Jim Zhou - MSFTModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Freitag, 28. August 2009 10:18Craigton TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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
    • Als Antwort markiertCraigton Mittwoch, 4. November 2009 18:34
    •  
  • Mittwoch, 4. November 2009 18:33Craigton TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    Just closing this off.
    • Als Antwort markiertCraigton Mittwoch, 4. November 2009 18:34
    •