I have 3 tables:
Degree(Id int(PK), DegreeName string)
1 Bachelor
2 Master
3 PhD
Track(Id int(PK), DegreeId int(FK), TrackName string)
1 2 Engineer
2 1 Technician
3 1 Assistant
4 2 Physicist
5 3 Doctor
Group(Id int(PK), TrackId int(FK), GroupName string)
1 4 Group1
2 3 Group2
3 1 Group3
4 3 Group4
5 2 Group5
there is a one to many relation between Degree and Track, and another one to many relation between Track and Group.
I have this class:
Public class DegreeDetails
{
public List<Track> TrackList { get; set; }
public List<Group> GroupsList { get; set; }
}
to get all tracks these belong to bachelor degree, which are technician and assistant, I use this code:
In the controller I use this code:
DegreeDetails MyView = new DegreeDetails();
MyView.TrackList = entity.Track.Where(s => s.DegreeID == 1).ToList();
how to get a Group List of all groups these study tracks belong to bachelor degree, which should be Group2, Group4 and Group5.
Probably the best thing to do is to join these tables. You could create a view in your database, or you can join tables using Linq.
This should work:
UPDATE If you want to use lambda exp, try this: