Solved

Different Linq synatx for this expression

Posted on 2010-11-16
7
255 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Device Location 4 29
Set value of dynamically created checkboxlist 2 32
SqlDependency to get update from sql to my c# app 2 39
Convert datetime to time string 10 21
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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