问题描述
i have a class
public class CloudFileVersion
{
public int FileVersionID { get; set; }
public int CloudFileID { get; set; }
public string FileName { get; set; }
public string DestinationPath { get; set; }
public string FileDate { get; set; }
public long FileSize { get; set; }
public string CreationDate { get; set; }
public int PlanID { get; set; }
public string CloudFileName { get; set; }
}
List<CloudFileVersion> objFileVersionlst
iam looping to find old file
int deleteDays=10;
foreach (var veritem in objFileVersionlst)
{
DateTime dtver = DateTime.ParseExact(veritem.FileDate, "yyyyMMddhhmmss", CultureInfo.InvariantCulture);
int ActualDays = (int)(DateTime.Now.ToUniversalTime() - dtver).TotalDays;
if ( ActualDays >= deleteDays)
{
//delete the file
}
}
我可以解决这个问题而无需循环使用linq
我尝试了什么:
目前iam像这样循环
foreach(var objFileVersionlst中的veritem)
{
DateTime dtver = DateTime.ParseExact(veritem.FileDate,yyyyMMddhhmmss,CultureInfo.InvariantCulture);
int ActualDays =(int)(DateTime.Now.ToUniversalTime() - dtver).TotalDays;
if(ActualDays> = deleteDays)
{
//删除文件
}
}
can i solve this problem without loop using linq
What I have tried:
currently iam looping like this
foreach (var veritem in objFileVersionlst)
{
DateTime dtver = DateTime.ParseExact(veritem.FileDate, "yyyyMMddhhmmss", CultureInfo.InvariantCulture);
int ActualDays = (int)(DateTime.Now.ToUniversalTime() - dtver).TotalDays;
if ( ActualDays >= deleteDays)
{
//delete the file
}
}
推荐答案
objFileVersionlst.Where(veritem=>(DateTime.Now.ToUniversalTime() - DateTime.ParseExact(veritem.FileDate, "yyyyMMddhhmmss", CultureInfo.InvariantCulture).TotalDays >= deleteDays).ForEach((veritem)=>{
// delete the file
});
这篇关于使用linq C#解决问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!