views:

139

answers:

2

I have 2 Entities "UserProfile" and "Agent", they are 1-many relationship. I want to do a query to get a list of Agents by giving the userProfileEntityKey. When I run it, I got this "The specified type member 'EntityKey' is not supported in LINQ to Entities" error.

public IQueryable<Agent> GetAgentListByUserProfile(EntityKey userProfileEntityKey)
{
ObjectQuery<Agent> agentObjects = this.DataContext.AgentSet;

IQueryable<Agent> resultQuery =
                    (from p in agentObjects
                     where p.UserProfile.EntityKey == userProfileEntityKey
                     select p);
    return resultQuery;
}

So, what is the correct way to do this? Do I use p.UserProfile.UserId = UserId ? If that's the case, it's not conceptual anymore. Or should I write object query instead of LINQ query?

A: 

Try this...

First, override the Equals() (and GetHashCode()) methods in your entity objects. The two entity objects should be equal if they are the same type and their IDs are equal.

Next, use this operation

public IQueryable<Agent> GetAgentListByUserProfile(UserProfile userProfile)
{
    var agentObjects = this.DataContext.AgentSet;
    var results = (from p in agentObjects
                   where p.UserProfile == userProfile
                   select p);
    return results;
}
Jarrett Meyer
+1  A: 

I would go the easer route p.UserProfile.UserId = UserId is your solution.

This is assuming that the UserId is your primary key for the table.

Searching by EntityKey does not make it conceptual. The conceptual piece comes when you are presenting a layer that presents your db layer in a different way.

Did i misunderstand your view of conceptual ?

Nix