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
C#.NET Programming

Avatar of undefined
Last Comment

8/22/2022 - Mon

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.


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

          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.

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

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

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?)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.