Query ManyMany Collections....

Thread is closed for posting
2 posts, 0 answers
  1. Steve
    Steve avatar
    1885 posts
    Member since:
    Dec 2008

    Posted 04 Feb 2011 Link to this post

    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'
  2. Petko_I
    Petko_I avatar
    184 posts

    Posted 08 Feb 2011 Link to this post

    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,
    the Telerik team
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Back to Top