Several WHERE in one LINQ 2 SQL method

I have a LINQ method that I am trying to create. This question seems to be the second WHERE clause. I get this error →

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<MatrixReloaded.Data.CMO.tblWorkerHistory>' to 'bool'

I also had &&vs there WHERE, but I was getting a similar error. I do not need anything tblWorkerHistoriesbut EndDate.

Between the two tables there attitude "Many-to-many" with EnrollmentIDboth FK on both.

public static DataTable GetCurrentWorkersByEnrollmentID(int enrollmentID)
    {
        using (var context = CmoDataContext.Create())
        {
            context.Log = Console.Out;

            var currentWorkers = from enrollment in context.tblCMOEnrollments
                                 where enrollment.EnrollmentID == enrollmentID
                                 where enrollment.tblWorkerHistories.Where(a => a.EndDate == null || a.EndDate > DateTime.Now)
                                 select
                                     new
                                         {
                                             enrollment.CMONurseID,
                                             enrollment.CMOSocialWorkerID,
                                             SupportWorkerName = enrollment.tblSupportWorker.FirstName + " " + enrollment.tblSupportWorker.LastName,
                                             SupportWorkerPhone = enrollment.tblSupportWorker.Phone
                                         };

            return currentWorkers.CopyLinqToDataTable();
        }
    }
+3
source share
2 answers

This is the problem:

where enrollment.tblWorkerHistories.Where(/* stuff */)

Where ... -, . Where?

, , Any Where... , , , , Any .

EDIT: , SQL , , LINQ , ? , , , Any , :

var currentWorkers = from enrollment in context.tblCMOEnrollments
                     where enrollment.EnrollmentID == enrollmentID
                     where enrollment.tblWorkerHistories.Any
                           (a => a.EndDate == null || a.EndDate > DateTime.Now)
                     select ...
+4

, .Any .Where ; .Where (.. where) , - try:

where enrollment.tblWorkerHistories.Any(
      a => a.EndDate == null || a.EndDate > DateTime.Now)
+4

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


All Articles