Solved

Different Linq synatx for this expression

Posted on 2010-11-16
7
257 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:o3h
  • 3
  • 2
  • 2
7 Comments
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34144916
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.
0
 
LVL 3

Expert Comment

by:kraiven
ID: 34144923
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.
0
 

Author Comment

by:o3h
ID: 34144956
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.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 19

Expert Comment

by:Bardobrave
ID: 34145157
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
0
 

Author Comment

by:o3h
ID: 34145216
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?)
0
 
LVL 3

Accepted Solution

by:
kraiven earned 500 total points
ID: 34145355
03h:

I think you need to let us know what your schema is. SelectMany is a multiple collection projection, but it looks to me like you want to do a join between Company and ReferencePerson table in which case you might more clearly model with the Linq join operator.

SelectMany example:
 
var list1 = new List<int>
{
	1,2,3,4
};

var list2 = new List<int> {1,2,3,4,5,6,7,8,9};

var q = from l1 in list1
		where li < 3
		from l2 in list2
		where l1 == l2
		select new {l1, l2};

Open in new window


the output here is:
1      1
2      2

Or using the Linq join syntax:
 
var list1 = new List<int>
{
	1,2,3,4
};

var list2 = new List<int> {1,2,3,4,5,6,7,8,9};

var q = from l1 in list1
		join l2 in list2 on l1 equals l2
		where l1 < 3
		select new {l1, l2};

Open in new window


the SelectMany allows you much more flexibility, but it looks like join is better suited.
0
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34146433
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net creating Contact in Outlook 1 57
Groupbox Control ? 2 29
Installing .NET 3.5 on Windows Server 2012 1 62
Connect Gridview column to Textbox in C# 2 41
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question