Solved

Different Linq synatx for this expression

Posted on 2010-11-16
7
253 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 19

Expert Comment

by:Bardobrave
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now