Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Different Linq synatx for this expression

Posted on 2010-11-16
7
256 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

809 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