向 LINQ-2-SQL 添加非 SQL where 子句

发布于 2024-10-22 05:47:14 字数 4187 浏览 1 评论 0原文

我有一个 Linq-2-SQL 可以更新 IEnumerable; LeftOverLocums 位于内部 foreach 循环之前。我需要在 Where 部分再添加一个布尔子句,但它与 SQL DB 无关。事实上,它是一个效用函数。请看下面的两个函数。第一个是主函数,第二个是我需要嵌入 Where 部分的函数。

    private void PreferenceFindJobs(ref JobCustomList jobList) {
        List<KeyValuePair<long, DateTime>> AlreadyPrefferedLocums = new List<KeyValuePair<long, DateTime>>();

        List<long> LocumsFilteringIDs = SchedulerMatrixStorage.Resources.Items.Select(col => Convert.ToInt64(col.Id)).ToList();

        IEnumerable<Locum> LeftOverLocums = null;

        foreach (JobCustom oneJob in jobList) {

            LeftOverLocums = from locums in DbContext.Locums
                                             join availabilities in DbContext.Availabilities on new {
                                                 OID = locums.OID
                                             } equals new {
                                                 OID = availabilities.LocumID
                                             }
                                             where
                                               LocumsFilteringIDs.Contains(locums.OID) &&
                                               locums.IsActive == true &&
                                               locums.IsLocumsExciteBan == false &&
                                               locums.IsGPHCBan == false &&
                                               locums.IsRPSGBBan == false &&
                                               locums.IsAdminMarkedComplete == true &&
                                               availabilities.AvailabilityStatusID == 1 &&
                                               availabilities.AvailableDate == oneJob.JobDate
                                             select locums;

            foreach (Locum oneLocum in LeftOverLocums) {
                    //This Locum can do this job

                    //Now check if he/she has been just alloted
                    if (AlreadyPrefferedLocums.Any(check => check.Key == oneLocum.OID && check.Value == oneJob.JobDate) == false) {
                        //No? Cool!
                        oneJob.LocumID = oneLocum.OID;
                        oneJob.LocumName = oneLocum.FirstName + " " + oneLocum.LastName;

                        //Add to the list to prevent double allocation
                        AlreadyPrefferedLocums.Add(new KeyValuePair<long, DateTime>(oneJob.LocumID, oneJob.JobDate));

                    } else {
                        continue;

                    }

            }

        }
    }

    public enum LocumType {
        Pharmacist = 1,
        Dispenser = 2,
        AccreditedCheckingTechnician = 3
    }

    public static Boolean IsJobTypeOK(Enumerations.LocumType _Job, Enumerations.LocumType _Locum) {
        bool ProcessResult;

        switch (_Job) {
            case  Enumerations.LocumType.Pharmacist:
            ProcessResult = _Locum == Enumerations.LocumType.Pharmacist;
            break;

            case Enumerations.LocumType.Dispenser:
            ProcessResult = _Locum == Enumerations.LocumType.Dispenser;
            break;

            case Enumerations.LocumType.AccreditedCheckingTechnician:
            ProcessResult = _Locum == Enumerations.LocumType.AccreditedCheckingTechnician || _Locum == Enumerations.LocumType.Dispenser;
            break;

            default:
            ProcessResult = false;
            break;
        }

        return ProcessResult;
    }

这是我想要的 Where 子句:

where
    IsJobTypeOK(oneJob.JobTypeID, locums.LocumTypeID) &&
    LocumsFilteringIDs.Contains(locums.OID) &&
    locums.IsActive == true &&
    locums.IsLocumsExciteBan == false &&
    locums.IsGPHCBan == false &&
    locums.IsRPSGBBan == false &&
    locums.IsAdminMarkedComplete == true &&
    availabilities.AvailabilityStatusID == 1 &&
    availabilities.AvailableDate == oneJob.JobDate

I have a Linq-2-SQL that updates the IEnumerable<Locum> LeftOverLocums before the inner foreach loop. I need to add one more boolean clause to the Where portion but it has nothing to do with SQL DB. In fact, its a utility function. Please see the two function below. First one is the main function and the second is the one I need to embed in the Where portion.

    private void PreferenceFindJobs(ref JobCustomList jobList) {
        List<KeyValuePair<long, DateTime>> AlreadyPrefferedLocums = new List<KeyValuePair<long, DateTime>>();

        List<long> LocumsFilteringIDs = SchedulerMatrixStorage.Resources.Items.Select(col => Convert.ToInt64(col.Id)).ToList();

        IEnumerable<Locum> LeftOverLocums = null;

        foreach (JobCustom oneJob in jobList) {

            LeftOverLocums = from locums in DbContext.Locums
                                             join availabilities in DbContext.Availabilities on new {
                                                 OID = locums.OID
                                             } equals new {
                                                 OID = availabilities.LocumID
                                             }
                                             where
                                               LocumsFilteringIDs.Contains(locums.OID) &&
                                               locums.IsActive == true &&
                                               locums.IsLocumsExciteBan == false &&
                                               locums.IsGPHCBan == false &&
                                               locums.IsRPSGBBan == false &&
                                               locums.IsAdminMarkedComplete == true &&
                                               availabilities.AvailabilityStatusID == 1 &&
                                               availabilities.AvailableDate == oneJob.JobDate
                                             select locums;

            foreach (Locum oneLocum in LeftOverLocums) {
                    //This Locum can do this job

                    //Now check if he/she has been just alloted
                    if (AlreadyPrefferedLocums.Any(check => check.Key == oneLocum.OID && check.Value == oneJob.JobDate) == false) {
                        //No? Cool!
                        oneJob.LocumID = oneLocum.OID;
                        oneJob.LocumName = oneLocum.FirstName + " " + oneLocum.LastName;

                        //Add to the list to prevent double allocation
                        AlreadyPrefferedLocums.Add(new KeyValuePair<long, DateTime>(oneJob.LocumID, oneJob.JobDate));

                    } else {
                        continue;

                    }

            }

        }
    }

    public enum LocumType {
        Pharmacist = 1,
        Dispenser = 2,
        AccreditedCheckingTechnician = 3
    }

    public static Boolean IsJobTypeOK(Enumerations.LocumType _Job, Enumerations.LocumType _Locum) {
        bool ProcessResult;

        switch (_Job) {
            case  Enumerations.LocumType.Pharmacist:
            ProcessResult = _Locum == Enumerations.LocumType.Pharmacist;
            break;

            case Enumerations.LocumType.Dispenser:
            ProcessResult = _Locum == Enumerations.LocumType.Dispenser;
            break;

            case Enumerations.LocumType.AccreditedCheckingTechnician:
            ProcessResult = _Locum == Enumerations.LocumType.AccreditedCheckingTechnician || _Locum == Enumerations.LocumType.Dispenser;
            break;

            default:
            ProcessResult = false;
            break;
        }

        return ProcessResult;
    }

This is my desired Where clause:

where
    IsJobTypeOK(oneJob.JobTypeID, locums.LocumTypeID) &&
    LocumsFilteringIDs.Contains(locums.OID) &&
    locums.IsActive == true &&
    locums.IsLocumsExciteBan == false &&
    locums.IsGPHCBan == false &&
    locums.IsRPSGBBan == false &&
    locums.IsAdminMarkedComplete == true &&
    availabilities.AvailabilityStatusID == 1 &&
    availabilities.AvailableDate == oneJob.JobDate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们的影子 2024-10-29 05:47:14

您根本不必使用“IsJobTypeOK”方法。


where
(
  (oneJob.JobTypeID == Enumerations.LocumType.AccreditedCheckingTechnician && 
     locums.LocumTypeID == Enumerations.LocumType.Dispenser) || 
  oneJob.JobTypeID == locums.LocumTypeID
) &&
LocumsFilteringIDs.Contains(locums.OID) &&
locums.IsActive == true &&
locums.IsLocumsExciteBan == false &&
locums.IsGPHCBan == false &&
locums.IsRPSGBBan == false &&
locums.IsAdminMarkedComplete == true &&
availabilities.AvailabilityStatusID == 1 &&
availabilities.AvailableDate == oneJob.JobDate

You dont have to use the method "IsJobTypeOK" at all.


where
(
  (oneJob.JobTypeID == Enumerations.LocumType.AccreditedCheckingTechnician && 
     locums.LocumTypeID == Enumerations.LocumType.Dispenser) || 
  oneJob.JobTypeID == locums.LocumTypeID
) &&
LocumsFilteringIDs.Contains(locums.OID) &&
locums.IsActive == true &&
locums.IsLocumsExciteBan == false &&
locums.IsGPHCBan == false &&
locums.IsRPSGBBan == false &&
locums.IsAdminMarkedComplete == true &&
availabilities.AvailabilityStatusID == 1 &&
availabilities.AvailableDate == oneJob.JobDate

横笛休吹塞上声 2024-10-29 05:47:14

最好的方法是在从数据库调用结果后过滤结果。

//Get you LeftOverLocums like you are now
//then
LeftOverLocums = LeftOverLocums
                 .Where(l => l.IsJobTypeOK(oneJob.JobTypeID, l.LocumTypeID))

数据库上不需要任何额外的东西

The best way to do this would be to filter the results after you call them from the database.

//Get you LeftOverLocums like you are now
//then
LeftOverLocums = LeftOverLocums
                 .Where(l => l.IsJobTypeOK(oneJob.JobTypeID, l.LocumTypeID))

Doesn't require anything extra on the database

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文