troubleshooting Question

ASP.NET and LINQ: How to Filter EntityDataSource Based on Associated Records / subquery?

Avatar of JMS1965
JMS1965Flag for United States of America asked on
ASP.NET
4 Comments1 Solution2871 ViewsLast Modified:
I am building my first web application, using ASP.NET 4 with VB. I'm also using LINQ to Entities.

The page I'm working on now is a listing of Contacts, a gridview that uses an EntityDataSource based on the "Person" entity.  This is a read-only list; no editing, adding or deleting. I've added some drop-down lists above the gridview to allow the user to filter the list, and am constructing the WHERE clause for the EntityDataSource in the code behind. So far, the user can filter the list based on Active/Inactive status (Boolean), Horse Owner status (Boolean) and/or on the first letter of the Contact Name. These filters are working properly.

My question involves the last filter I'd like to add. Each contact record can have associated records in the PeopleRoles table … for example, a contact could have no PeopleRoles records, or could have one for RoleType "Stable", or one for "Trainer", or could have both a "Stable" and a "Trainer" record in the PeopleRoles table.

I've added a drop-down list for this in the filter area; the items in the drop-down list are populated dynamically from the user's list of possible PeopleRoles.  But, I don't know how to wire this so that the selection of a specific role from that list re-filters the EntityDataSource to include only Contacts who have that associated role.

I have set the "Include" property of the EntityDataSource to include the necessary Navigation item "PeopleRoles", but this of course returns a collection of records for each contact. Is there a way to construct the Where property to return only Contacts who have a PeopleRoles record of the selected type? I'm thinking it might require a subquery or nested query, but am not sure where to start.

Below is the code-behind function that is called whenever any of the filtering drop-down lists have a new item selected. All are working except the "filter on roles" section … you'll see my initial attempt there on line 16.  In testing this generates the error: "'RoleID' is not a member of 'Transient.collection[edmIFAcore.PeopleRole(Nullable=True,DefaultValue=)]'. To extract a property of a collection element, use a subquery to iterate over the collection."

    Private Sub FilterGridView()
        Dim strWhere As String = "it.AcctID=" & Profile.iFAacctID

        'filter on horse owners
        Select Case ddlOwners.SelectedItem.Text
            Case "[All]"

            Case "Horse Owners Only"
                strWhere = strWhere & " AND it.HorseOwner=True"
            Case "Non-Horse Owners Only"
                strWhere = strWhere & " AND it.HorseOwner=False"
        End Select

        'filter on roles
        If ddlRoles.SelectedItem.Text <> "[All]" Then
            strWhere = strWhere & " AND it.PeopleRoles.RoleID=" & ddlRoles.SelectedValue
        End If

        'filter on first letter
        If ddlStartsWith.SelectedItem.Text <> "[All]" Then
            strWhere = strWhere & " AND it.fldClientName LIKE '" & ddlStartsWith.SelectedItem.Text & "%'"
        End If

        'filter on active status
        Select Case ddlActives.SelectedItem.Text
            Case "[All]"

            Case "Actives Only"
                strWhere = strWhere & " AND it.Inactive=False"
            Case "Inactives Only"
                strWhere = strWhere & " AND it.Inactive=True"
        End Select

        'apply filter and sorting to the EntityDataSource
        edsContactList.Where = strWhere
        If Len(edsContactList.OrderBy) > 0 Then
            edsContactList.OrderBy = edsContactList.OrderBy
        Else
            edsContactList.OrderBy = "it.fldClientName"
        End If

    End Sub

Open in new window


Thanks very much for any direction you can provide!
ASKER CERTIFIED SOLUTION
JMS1965

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros