使用linq C#解决问题 [英] Solve the problem with linq C#

查看:80
本文介绍了使用linq C#解决问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
}

}

推荐答案

Linq不是一个解决所有问题的全部和全部解决方案 - 它是一大堆工具中的工具!

Wh在你试图做的是删除文件,如果他们已经在那里太久了。这是一个程序性的东西,适用于某些文件而不是其他文件,而不是特定的组。

虽然你可以在这里使用Linq来帮助你 - 一种将文件列表缩小到的Where方法只是那些需要删除的东西 - 它实际上比你现在的版本效率低,因为它需要执行新的分配来保存新对象集合。



如果现有代码有效,请不要管它 - Linq不会以任何实质方式改进它。



BTW:将文件和创建日期存储为字符串是非常的,非常糟糕的想法 - 将它们存储为DateTime值,您可以更轻松地使用和理解整个代码。
Linq is not a "be all and end all" solution to all problems - it's a tool in a big bag of tools!
What you are trying to do is remove files if they have been there for too long. That's a procedural thing which is applied to some files and not others, not a "group" thing specifically.
While you can use Linq here to help you - a Where method to shrink your list of files to just those needing deletion - it is in fact less efficient than the version you have now because it requires new allocations to be performed to hold the "new" collection of objects.

If you existing code works, leave it alone - Linq will not improve it in any material way.

BTW: storing File and creation dates as string is a very, very poor idea - store them as DateTime values and you whole code becomes easier to work with and understand.


objFileVersionlst.Where(veritem=>(DateTime.Now.ToUniversalTime() - DateTime.ParseExact(veritem.FileDate, "yyyyMMddhhmmss", CultureInfo.InvariantCulture).TotalDays >= deleteDays).ForEach((veritem)=>{
// delete the file
});


这篇关于使用linq C#解决问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆