Link to home
Start Free TrialLog in
Avatar of o3h
o3h

asked on

Different Linq synatx for this expression

Hi I wonder how you make this Linq expression:
DataContext.GetObjectContext().Company.Where(i => i.Id == selectedAssignmentID).SelectMany(i => i.ReferencePersons).ToList<ReferencePerson>();

Into from iin DataContext.GetObjectContext().Company .....................

I just cant figure out how to use Selectmany with the other syntax. Thx
Avatar of Bardobrave
Bardobrave
Flag of Spain image

Let's see... if I understand it you are trying to get a list of ReferencePersons on your Company where company id is equal to selected assignment id, isn't it?

What I usually do for this situations is use direct one to many linking:

DataContext.GetObjectContext().Company.Where(i => i.Id == selectedAssignmentID).ReferencePerson.ToList<ReferencePerson>();

I use to stablish database relationships between my tables, so:
DataContext.GetObjectContext().Company.Where(i => i.Id == selectedAssignmentID)
will return a list of those Companies that matches the idAssignment expression.

As a company can have many reference persons and I have a database relationship that stablish this on my Linq generated data model

DataContext.GetObjectContext().Company.Where(i => i.Id == selectedAssignmentID).ReferencePerson

will directly return the list of reference persons of all companies that matches the where clause.

Without direct relationships on database I usually use joins to achieve this effect, never tried before with selectMany.
Avatar of kraiven
kraiven

Hi,

Linq SelectMany is equivalent to multiple from statements in the syntax sugared Linq style.

e.g.
          from a in DataA
          from b in DataB
          select new {a.Prop, b.Prop}

Not sure of your precise data schema but I hope this helps.
Avatar of o3h

ASKER

I want the exact same behavior as the first line. Im just curious how your write in the following "syntax":

from comp in DataContext.GetObjectContext().Company
                                    where comp.Id == selectedAssignmentID
                                    select comp.ReferencePersons..................... (some selectmany stuff)

Sorry if I was unclear.
from comp in DataContext.GetObjectContext().Company
                                    where comp.Id == selectedAssignmentID
                                    select comp.ReferencePersons

Is the same as

DataContext.GetObjectContext().Company.Where(i => i.Id == selectedAssignmentID).ReferencePersons
Avatar of o3h

ASKER

Yes but how do i Do SelectMany?
So the following would be valid:

List<ReferencePerson> referencePersons = (from comp in DataContext.GetObjectContext().Company
                                    where comp.Id == selectedAssignmentID
                                    select comp.ReferencePersons)............. (some extra?)
ASKER CERTIFIED SOLUTION
Avatar of kraiven
kraiven

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I do not know what do you mean with "select many".

This will return you a list of elements of ReferencePerson class with all it's data and fields loaded into each one of the list's elements:

List<ReferencePerson> referencePersons = DataContext.GetObjectContext().Company.Where(i => i.Id == selectedAssignmentID).ReferencePersons

Maybe if you tell us what fields/data/info are you trying to reach we can offer you a solution/alternative.