locked
Filter EntityDataSource based on foreign key RRS feed

  • Question

  • Hi,
     
    I am trying to do something simple but I just can't figure it out. Please can you help!

    My schema looks like this:

    CustomerOrder
      Id (pk, guid)
      AgentId (fk, guid, nullable)
      ... other fields ...

    Agent
      Id (pk, guid)
      Name (string)
      ... other fields ...

    I need to create an EntityDataSource in my c# asp.net app that displays a list of orders for the logged-in agent. The agent's ID is stored in the Session collection during login.

    Currently, my EntityDataSource looks like this:

    <asp:EntityDataSource ID="customerOrdersDataSource" runat="server"   
            ConnectionString="name=MyEntities"   
            DefaultContainerName="MyEntities" EntitySetName="CustomerOrder"   
            Select="it.[Id], it.[Title], it.[FirstName], it.[LastName], it.[HomeTelephone], it.[Postcode], it.[Status]"   
            Where="(it.[Agent] = @AgentId)" 
            Include="Agent" 
            EntityTypeFilter="">  
            <WhereParameters> 
              <asp:SessionParameter Name="AgentId" SessionField="LoggedInAgentId" Type="String" /> 
            </WhereParameters> 
        </asp:EntityDataSource> 

    However, this throws the following exception: The argument types 'xxxModel.Agent' and 'Edm.String' are incompatible for this operation., near WHERE predicate, line 8, column 13.

    I've tried using a Guid and an instiated object but I can't get it going.

    All I need to do is filter based on a foreign key. Of course, VS2008 removes the native (guid) foreign key fields from the model and replaces them with associations instead.

    Any ideas?

    Thanks
    Saturday, February 14, 2009 1:22 AM

Answers

  • Seems like you are trying to compare the entire Agent instance to the string ID, try changing your Where expression to be

    it.Agent.Id = @AgentId 

    Thanks,
    jeff
    • Proposed as answer by Jiri {x2} CincuraMVP Saturday, February 14, 2009 7:45 PM
    • Marked as answer by sigol Wednesday, March 11, 2009 7:54 PM
    Saturday, February 14, 2009 2:29 AM

All replies

  • Seems like you are trying to compare the entire Agent instance to the string ID, try changing your Where expression to be

    it.Agent.Id = @AgentId 

    Thanks,
    jeff
    • Proposed as answer by Jiri {x2} CincuraMVP Saturday, February 14, 2009 7:45 PM
    • Marked as answer by sigol Wednesday, March 11, 2009 7:54 PM
    Saturday, February 14, 2009 2:29 AM
  • Thank you - and sorry for the delay!
    Wednesday, March 11, 2009 7:54 PM