Adding a where clause for nested Linq selects

I'm still new to Linq, so if you see something that I really shouldn't do, feel free to suggest changes.

I am working on a new system that allows officers to sign up for overtime work. Part of the data is displayed on a map with search criteria that filter unwanted positions. To simplify the work with data, it is read into the structure of hierarchy objects using Linq. In this example, a job can contain several shifts, and each shift can have several positions. The Linq statement to read them is as follows.

        var jobs = (from j in db.Job
                    join s in db.Shift on j.Id equals s.JobId into shifts
                    select new JobSearchResult
                    {
                        JobNumber = j.Id,
                        Name = j.JobName,
                        Latitude = j.LocationLatitude,
                        Longitude = j.LocationLongitude,
                        Address = j.AddressLine1,
                        Shifts = (from shift in shifts
                                  join p in db.Position on shift.Id equals p.ShiftId into positions
                                  select new ShiftSearchResult
                                  {
                                      Id = shift.Id,
                                      Title = shift.ShiftTitle,
                                      StartTime = shift.StartTime,
                                      EndTime = shift.EndTime,
                                      Positions = (from position in positions
                                                   select new PositionSearchResult
                                                   {
                                                       Id = position.Id,
                                                       Status = position.Status
                                                   }).ToList()
                                  }).ToList()
                    });

. , , . . . , , , . , .

jobs = jobs.Where(j => j.JobNumber == 1234);

, Shifts Positions. , , ? - , , () .

jobs = jobs.Shifts.Where(s = s.StartTime > JobSearch.StartTime)      //JobSearch.StartTime is a form variable.

- ?

+3
1

1: , , EntitySet. http://msdn.microsoft.com/en-us/library/bb629295.aspx

2: . 3 . , .

(). 3 . .

Expression<Func<Position, bool>> PositionFilterExpression =
  p => p.Status == "Open";

Expression<Func<Shift, bool>> ShiftFilterExpression =
  s => s.Positions.Where(PositionFilterExpression).Count == 3  

Expression<Func<Job, bool>> JobFilterExpression =
  j => true

3: :

   List<JobSearchResult> jobs = db.Jobs
     .Where(JobFilterExpression)
     .Select(j => new JobSearchResult
     { 
       JobNumber = j.Id, 
       Name = j.JobName, 
       Latitude = j.LocationLatitude, 
       Longitude = j.LocationLongitude, 
       Address = j.AddressLine1, 
       Shifts = j.Shifts
         .Where(ShiftFilterExpression)
         .Select(s => new ShiftSearchResult
         {
           Id = s.Id,
           Title = s.ShiftTitle,
           StartTime = s.StartTime,
           EndTime = s.EndTime,
           Positions = s.Positions
             .Where(PositionFilterExpression)
             .Select(p => new PositionSearchResult
             {
               Id = position.Id,
               Status = position.Status
             })
             .ToList() 
         })
         .ToList()
     })
     .ToList(); 
+1

Source: https://habr.com/ru/post/1752473/


All Articles