Querying accross relationships... RRS feed

  • Question

  • I'm having problems when trying to query accross an EntitySet/Ref relationship generated by LINQ to SQL.  It can be explained here...

    Either something is broken or I'm missing something.

    Friday, January 23, 2009 11:24 PM

All replies

  • Once you retrieve 'cat' in the first query, the 'Products' property is a deferred collection not a query, its an IEnumerable<Product> but not an IQueryable<Product>.  Enumerating it in any way will cause a fixed query to execute and retrieve all the products for that category.  Any query you apply to it will be executing using LINQ to Objects against that collection and occur locally.

    What you can do instead, is to query the remote products collection directly.

    var cat = db.Categories.Where(c => c.ID == 1).SingleOrDefault();

    var products = db.Products.Where(p => p.Category == cat).Skip(1).Take(1).SingleOrDefault();


    Wayward LINQ Lacky
    Saturday, January 24, 2009 5:42 AM