views:

102

answers:

1

Hi,

I've created a Model with Entity Framework from three database tables:

  1. Agents
  2. AgentsGroups
  3. Groups

AgentsGroups is a simple table with three columns: 1 id and two foreign keys linking Agents and Groups (every Agent can have multiple Groups). Pretty basic stuff.

Entity Framework correctly recognizes the relationships between the table. Now, with LINQPad I am able to get the names of all the groups associated with an agent starting from the agent ID:

from a in Agents
    join ag in AgentsGroups on a.Code equals ag.AgentCode
    join g in Groups on ag.GroupCode equals g.Code
    where a.Code == 10199
    select g.Name

This, though, doesn't work on the very program as, in fact, AgentCode and GroupCode are mapped as Associations, not fields.

I guess I have to use Include, but I've never used it, so the help requested is: how could I translate the given semi-working linq expression in a similar expression giving out the Group Names but using Associations?

Thanks in advance

+2  A: 

Much simpler in EF:

from a in Agents
where a.Code == 10199
from g in a.Groups
select g.Name

You almost never use join in EF.

Craig Stuntz
hi, thanks for the answer, but the given expression gave me ALL the rows in group.. where is the association? is it meant to be implicit?
pistacchio
Look carefully at the qualifiers (a., for example). It's not implicit, just small. I was guessing on the association property name; you may have called it something else.
Craig Stuntz