Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple WHERE's in same LINQ 2 SQL Method

I have the below LINQ Method I am trying to create. The issue seems to be the Second WHERE clause. I am getting this error -->

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

I also had && there vs WHERE but I was getting a similar error. I don't NEED anything from tblWorkerHistories except the EndDate stuff.

There is a Many To Many relationship between the 2 tables with EnrollmentID being a 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();
        }
    }
like image 660
Refracted Paladin Avatar asked Aug 01 '26 21:08

Refracted Paladin


1 Answers

This is the problem:

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

Where returns a sequence... whereas you need something that will return a Boolean value. What are you trying to do with that embedded Where clause?

As Marc says, it could be that you just need an Any call instead of Where... but if you could explain what you're trying to do, that would make it a lot easier to help you. Note that Any does return a Boolean value, instead of a sequence.

EDIT: Okay, so in SQL you'd use a join, but you don't need an explicit join here because LINQ is implicitly doing that for you, right? If you're trying to find enrollments where any of the histories match the date, and you don't care about the histories themselves, then Any is indeed what you want:

var currentWorkers = from enrollment in context.tblCMOEnrollments
                     where enrollment.EnrollmentID == enrollmentID
                     where enrollment.tblWorkerHistories.Any
                           (a => a.EndDate == null || a.EndDate > DateTime.Now)
                     select ...
like image 130
Jon Skeet Avatar answered Aug 03 '26 11:08

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!