This is a migrated thread and some comments may be shown as answers.

Query ManyMany Collections....

1 Answer 51 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 04 Feb 2011, 08:17 PM
Tables are Locations, and Rotations

A rotation can have many locations, and visa versa

So if I have a locationID of 5, how do I query to get a list of all rotations who are linked to the LocationID 5?

I'm very confused :)  This is all I can think of....

var rotations = (from r in authContext.CommonRotations
                                 where r.CommonLocations.Where(x => x.LocationID == 3)
                                 select r);

...but that doesn't compile
CS0029: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AuthDB.Model.CommonLocation>' to 'bool'

1 Answer, 1 is accepted

Sort by
0
Petko_I
Telerik team
answered on 08 Feb 2011, 08:05 PM
Hi Steve,

You can first find all CommonLocations with a LocationID of value 5 and then use SelectMany to load all related CommonRotations into one flattened result:
var query = context.CommonLocations
             .Where(x => x.LocationID == 5)
             .SelectMany(x => x.CommonRotations);
The idea is to use the filtering clause easily and then merge the lists of CommonRotations into a single collection.

We do hope the query provided is helpful. Should you have further questions, feel free to contact us.

All the best,
Petko_I
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Tags
LINQ (LINQ specific questions)
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
Petko_I
Telerik team
Share this question
or