如何遍历DateTime列表并将其作为参数发送给函数

本文关键字:参数 函数 何遍历 遍历 列表 DateTime | 更新日期: 2023-09-27 18:26:36

如何迭代返回DateTime list的列表,并将单个日期传递给需要DateTime参数的函数?以下是我的LINQ查询,返回日期列表:

var lastModifiedDates = context.Reviews
                               .Where(s => s.IsActive == true)
                               .Where(s => s.ReviewStatusId == (int)ReviewStatuses.EmployeeSignature)
                               .Select(v => v.ModifiedDate)
                               .ToList();

以下是我如何通过日期:

foreach (var lastModifiedDate in lastModifiedDates)
{
    AddBusinessDay(lastModifiedDate, 15);                 
}

以下是AddBusinessDay函数的样子:

public static DateTime AddBusinessDay (DateTime date, int days)

AddBusinessDay(lastModifiedDate, 15);上,我得到以下错误:

错误28"EvaluationFormAccess.AddBusinessDay(System.DateTime,int)"的最佳重载方法匹配包含一些无效参数

审查实体:

    public int ReviewId { get; set; }
    public System.Guid ReviewGuid { get; set; }
    public int ReviewStatusId { get; set; }
    public int PersonId { get; set; }
    public int SupervisorId { get; set; }
    public int ReviewYearId { get; set; }
    public Nullable<bool> EmployeeOnLeave { get; set; }
    public Nullable<bool> PlanInPlace { get; set; }
    public string EmployeeComments { get; set; }
    public string EmployeeSignature { get; set; }
    public Nullable<System.DateTime> EmployeeSignDate { get; set; }
    public string ManagerComments { get; set; }
    public string ManagerSignature { get; set; }
    public Nullable<System.DateTime> ManagerSignDate { get; set; }
    public System.DateTime CreatedDate { get; set; }
    public System.Guid CreatedBy { get; set; }
    public Nullable<System.DateTime> ModifiedDate { get; set; }
    public Nullable<System.Guid> ModifiedBy { get; set; }
    public bool IsActive { get; set; }

如何遍历DateTime列表并将其作为参数发送给函数

您的ModifiedDate属性实际上是一个Nullable<DateTime>(也可以写成DateTime?),它实际上根本不是一个DateTime

你有两个选择:

在LINQ查询中将可能的DateTime解析为实际的DateTime

var lastModifiedDates = context.Reviews
                        .Where(s => s.IsActive == true)
                        .Where(s => s.ReviewStatusId == (int)ReviewStatuses.EmployeeSignature)
                        .Where(v => v.ModifiedDate.HasValue)
                        .Select(v => v.ModifiedDate.Value)
                        .ToList();

或者在你的循环中进行:

foreach (var lastModifiedDate in lastModifiedDates)
{
    if (lastModifiedDate.HasValue)
        AddBusinessDay(lastModifiedDate.Value, 15);                 
}

Ksv3n 提供的环路解决方案