如何遍历DateTime列表并作为参数

时间:2015-12-11 16:27:38

标签: c# linq datetime

如何遍历返回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; }

1 个答案:

答案 0 :(得分:2)

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

您有两种选择:

可能的 DateTime解析为LINQ查询中的实际 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

提供