none
Limit number of properties returned by LINQ to Entity query RRS feed

  • Question

  • Hi,

    I am trying to limit the number of properties (columns) returned from a LINQ to Entity query so that it only returns the SiteClientID and ClientName since I will use the data to populate a DropDown list box and so do not need all the properties in the object.

    I am getting this error:

    Unable to cast the type 'System.Data.Common.DbDataRecord' to type 'DocketHubTest.SiteClient'. LINQ to Entities only supports casting Entity Data Model primitive types.

    Server Error in '/' Application.
    QueryCreated event returned a query of type 'ObjectQuery`1' when type 'ObjectQuery`1' is required.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: QueryCreated event returned a query of type 'ObjectQuery`1' when type 'ObjectQuery`1' is required.

    +++++++++++++++++++++++++++++++++++++++

    This is the code I am using:

    protected void SiteEDS_QueryCreated(object sender, QueryCreatedEventArgs e)
    {
           var sites = e.Query.Cast<SiteClient>();

           e.Query = from c in sites
                       select new SiteClient 
                       {
                           SiteClientID = c.SiteClientID,
                           ClientName = c.ClientName 
                       };
     }

    Could someone please shed some light about what is wrong?

    Cheers

    C

    Wednesday, March 21, 2012 10:14 AM

Answers

All replies

  • Hi Claudio,

    To only retrieve the fields you need from the DB you should use the EntityDataSource.Select property, like so:

    <asp:EntityDataSource Select="it.[SiteClientID], it.[ClientName]" (other properties...)>

    This will cause the EntityDataSource to project the SiteClient into an anonymous type with just those fields. Note that once you make this change the type of e.Query will no longer be a collection of SiteClients, so you'll need to remove the code you've posted or you'll continue getting an exception.

    Regards,

    Tyler

    Wednesday, March 21, 2012 11:08 AM
  • Hi Tyler,

    I have tried that but since I have set the EntityDataSource to allow Update and delete it does not allow me to use the Select statement.

    Server Error in '/' Application.
    --------------------------------------------------------------------------------

    Select cannot be set if EnableDelete, EnableInsert, or EnableUpdate is enabled.

    Any other suggestion?

    <asp:EntityDataSource ID="SiteClientMailingEDS" runat="server" ContextTypeName="DocketHubTest.DocketHubLabellerEntities"
            EnableFlattening="false" AutoGenerateWhereClause="true" EntitySetName="SiteClientMailings"
            EnableUpdate="true" EnableDelete="true"
            Select="it.[ClientName], it.[PlannedMailingDate], it.[MailingStatus], it.[ActiveFlag], it.[TotalPrice]"
            >
            <WhereParameters>
                <asp:ControlParameter ControlID="dllSite" Name="SiteClientID" PropertyName="SelectedValue"
                    Type="Int32" />
            </WhereParameters>
        </asp:EntityDataSource>

    Cheers

    C

    Wednesday, March 21, 2012 11:15 AM
  •  Maybe you go to the code behind file and do things and not in the
    ASP.NET HTML page.
     
    Wednesday, March 21, 2012 5:27 PM
  • If you read the full post you will see that I have tried it in code behind.
    Wednesday, March 21, 2012 7:40 PM
  • Hi Claudio Pallone,

    Welcome to MSDN Forum.

    Based on the issue, I suggest you to post in ASP.NET Data Access Forum. There are mor specialists of ASP.NET Data Access there, I think you can get help more effectively.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, March 23, 2012 5:19 AM
    Moderator