1

So I have this database model :

Student<->StudentClasses<->Classes

where 1 student is linked to many StudentClasses and one Class is linked to many StudentClasses.

How do I write a LINQ query to get all the classes linked to the student with Id 1 ?

the following query throws an exception ("Can only specify query options (orderby, where, take, skip) after last navigation.") :

                     var qry = from sc in service.StudentClasses
                      where sc.StudentId == 1
                      from c in service.Classes
                      where c.ClassId == sc.StudentId
                      select c;
Attilah
  • 17,632
  • 38
  • 139
  • 202
  • 1
    This is a simliar problem to here: http://stackoverflow.com/questions/5746992/linq-query-error/5747842#5747842 LINQ doesn't support what you are trying to do. So you need to either create multiple queries, or expand and pull back more than what you need. – Nix Apr 27 '11 at 16:14
  • are you using linq to sql? or another ORM? – TimC Apr 27 '11 at 16:23
  • 1
    He is using LINQ to ODATA/rest. – Nix Apr 27 '11 at 16:24

1 Answers1

0

This would work, but it will crash if you the ID DNE.

var qry =  service.StudentClasses
              .Expand("Classes")
              .Where(x=>x.StudentId==1)
              .First()
              .Classes.Select(t=>t);
Nix
  • 57,072
  • 29
  • 149
  • 198
  • Is there a better way to expose your database than using WCF Data Services ? – Attilah Apr 27 '11 at 16:38
  • Depends, if you are doing simple thin client stuff, and have no business logic, DataServices are the way to go, if you are going to be doing complex queries, create some custom queries. I dont like the client side API for data services and I dont like giving clients the ability to query my data/change my data in any way they choose. As a result I tend to control how people access my data via web services and data access layers. – Nix Apr 27 '11 at 16:47