簡體   English   中英

在 linq where 子句中使用方法返回值

[英]Use method return value inside linq where clause

在這里,我有一個 linq 查詢,它工作正常,但我試圖讓它更具可讀性。 基本上,我將新的 DateTime 范圍添加到數據庫中,但它不能與現有的相交。 我嘗試編寫一個方法,傳遞開始和結束時間並在 where 子句中使用返回值,但據我了解,不能在查詢中使用 function。 對此有何建議?

var salariesInPeriod = await db.Salaries
                .Where(x => x.Id != salaryIdToIgnore)
                .Where(x => x.UserId == userId)
                .Where(x =>
                    //These filters check if there are any overlapping ends
                (x.StartDate <= startDate &&
                 startDate <= (x.EndDate ?? DateTime.MaxValue))
                ||
                (x.StartDate <= (endDate ?? DateTime.MaxValue) &&
                 (endDate ?? DateTime.MaxValue) <= (x.EndDate ?? DateTime.MaxValue))
                ||
                (startDate <= x.StartDate &&
                 x.StartDate <= (endDate ?? DateTime.MaxValue))
                ||
                (startDate <= (x.EndDate ?? DateTime.MaxValue) &&
                 (x.EndDate ?? DateTime.MaxValue) <= (endDate ?? DateTime.MaxValue))
                )

                .Select(x => new
                {
                    x.StartDate,
                    x.EndDate
                })
                .ToListAsync();

這就是我嘗試過的:

public bool CheckIntersections(DateTime currentStart, DateTime newStart, DateTime? currentEnd, DateTime? newEnd)
    {
        if ((currentStart <= newStart &&
             newStart <= (currentEnd ?? DateTime.MaxValue))
            ||
            (currentStart <= (newEnd ?? DateTime.MaxValue) &&
             (newEnd ?? DateTime.MaxValue) <= (currentEnd ?? DateTime.MaxValue))
            ||
            (newStart <= currentStart &&
             currentStart <= (newEnd ?? DateTime.MaxValue))
            ||
            (newStart <= (currentEnd ?? DateTime.MaxValue) &&
             (currentEnd ?? DateTime.MaxValue) <= (newEnd ?? DateTime.MaxValue)))
        {
            return true;
        }

        return false;
    }

然后我嘗試在查詢中使用結果:

var salariesInPeriod = await db.Salaries
            .Where(x => x.Id != salaryIdToIgnore)
            .Where(x => x.UserId == userId)
            .Where(x => CheckIntersections(x.StartDate, startDate, x.EndDate,endDate) == true)

            .Select(x => new
            {
                x.StartDate,
                x.EndDate
            })
            .ToListAsync();

任何想法如何簡化該查詢?

您不能在 linq 查詢中使用任何 function。 為了更好的可讀性,您可以使用擴展方法:

public static class SalaryQueryExtensions
{
    public static IQueryable<Salary> WithIntersections(
        this IQueryable<Salary> salaries, DateTime currentStart, DateTime newStart, DateTime? currentEnd, DateTime? newEnd)
    {
        // return your query. Example:
        // salaries.Where(x=> x.DateTime > currentStart);
    }

    public static IQueryable<Salary> WithIgnoreId(this IQueryable<Salary> salaries, Guid id)
    {
        // return your query.
    }

    public static IQueryable<Salary> WithUserID(this IQueryable<Salary> salaries, Guid userId)
    {
        // return your query.
    }
}

然后使用這樣的擴展方法:

var salariesInPeriod = await db.Salaries
            .WithIgnoreId(salaryIdToIgnore)
            .WithUserId(userId)
            .WithIntersections(startDate,endDate)
            .ToListAsync();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM