Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
}

}


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
}

}
Posted
Updated 29-Sep-16 22:35pm
v2
Comments
Maciej Los 30-Sep-16 3:58am    
Why you want to Linq solution, does for loop is worse than that?
srilekhamenon 30-Sep-16 4:00am    
for clean code
Mehdi Gholam 30-Sep-16 4:06am    
If your current code works, don't change it.
Maciej Los 30-Sep-16 4:38am    
Do you think that Linq solution is more clear than standard for loop? Wrong!

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.
 
Share this answer
 
C#
objFileVersionlst.Where(veritem=>(DateTime.Now.ToUniversalTime() - DateTime.ParseExact(veritem.FileDate, "yyyyMMddhhmmss", CultureInfo.InvariantCulture).TotalDays >= deleteDays).ForEach((veritem)=>{
// delete the file
});
 
Share this answer
 
Comments
Maciej Los 30-Sep-16 4:37am    
Correct answer, but the question is still active: why to change some code to linq solution if it works?
Midi_Mick 30-Sep-16 4:56am    
That's none of my business - I don't need to know the reason people want (or need) to do something in order to help them do it if I am able.
Maciej Los 30-Sep-16 5:32am    
I understand your point of view, but i campletely agree with OriginalGriff, even if i'm fan of Linq.
Cheers,
Maciej
Karthik_Mahalingam 30-Sep-16 5:27am    
5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900