Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何遍历日期时间列表并将其作为参数发送给函数_C#_Linq_Datetime - Fatal编程技术网

C# 如何遍历日期时间列表并将其作为参数发送给函数

C# 如何遍历日期时间列表并将其作为参数发送给函数,c#,linq,datetime,C#,Linq,Datetime,如何遍历返回DateTime列表的列表,并将各个日期传递给需要DateTime参数的函数? 下面是返回日期列表的LINQ查询: var lastModifiedDates = context.Reviews .Where(s => s.IsActive == true) .Where(s => s.ReviewStatusId == (int)ReviewSta

如何遍历返回DateTime列表的列表,并将各个日期传递给需要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; }
public int ReviewId{get;set;}
public System.Guid ReviewGuid{get;set;}
public int ReviewStatusId{get;set;}
公共int PersonId{get;set;}
public int-SupervisorId{get;set;}
public int ReviewYearId{get;set;}
公共可为空的EmployeeOnLeave{get;set;}
公共可为空的PlanInPlace{get;set;}
公共字符串EmployeeComments{get;set;}
公共字符串EmployeeSignature{get;set;}
公共可为空的EmployeeSignDate{get;set;}
公共字符串管理器命令{get;set;}
公共字符串管理器签名{get;set;}
公共可为空的ManagerSignDate{get;set;}
public System.DateTime CreatedDate{get;set;}
public System.Guid由{get;set;}创建
公共可为空的ModifiedDate{get;set;}
公共可为null的ModifiedBy{get;set;}
公共bool IsActive{get;set;}

您的
ModifiedDate
属性实际上是一个
可空的
(也可以写成
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);                 
}

循环解决方案由

提供,v.ModifiedDate的类型是什么?在循环结束时,您也不需要使用
.ToList()
。只要
IEnumerable
就足以让您
foreach
继续。@HereToLearn_uu那么这就没有意义了。您确定它不是
日期时间吗?
-请注意
。只需进行三重检查:在调用该方法之前,编写
var t=lastModifiedDate().GetType()
,在其上插入一个断点,并向我们显示结果eah。。从你的编辑来看,这绝对不是日期时间。将查询更改为:
.Where(v=>v.ModifiedDate.HasValue)。选择(v=>v.ModifiedDate.Value)@Ksv3n继续,取消删除答案,伙计-无论如何,我们在同一时间得出了相同的结论:)