none
how to avoid for each next loops with linq RRS feed

  • Question

  •       
    
                Dim ToBeDeletedFoundOldLogFiles = (From fl In My.Computer.FileSystem.GetFiles(Global_asax.ide.xmlfps)
                Where (DateTime.Now.Subtract(File.GetLastWriteTime(fl)).TotalDays > 5) _
                And Path.GetFileNameWithoutExtension(fl).StartsWith("rts485log created")
                Select fl)
                ' getcreationtime doesnt change with renaming 
    
                'And System.IO.Path.GetFileNameWithoutExtension(file).StartsWith("rts485log created")
                'GetCreationTime
    
                For Each fl In ToBeDeletedFoundOldLogFiles
                              FileIO.FileSystem.DeleteFile(file)
    
                Next
    

    i use here linq as a query , but how to include the following  for each next loop in the linq code , it must be possible. 
    nattelip
    • Edited by lipje Friday, January 6, 2012 8:26 PM
    Friday, January 6, 2012 8:25 PM

Answers

  • The way you have this written is actually what I would consider the "best".

     

    The issue here is that your For Each loop is intended to perform an action on each item.  Your LINQ query is exactly that - a "query".  Mixing the two means you create a query that has side effects, which is not necessarily a good idea.  See: http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx

     

    That being said, you can do this, via List(Of T).ForEach:

    (From fl In My.Computer.FileSystem.GetFiles(Global_asax.ide.xmlfps)
                Where (DateTime.Now.Subtract(File.GetLastWriteTime(fl)).TotalDays > 5) _
                And Path.GetFileNameWithoutExtension(fl).StartsWith("rts485log created")
                Select fl).ToList()
                .ForEach(Sub(file) FileIO.FileSystem.DeleteFile(file))
    


    However, as the article above demonstrates, what you have is likely a better, more maintainable option.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by lipje Friday, January 6, 2012 11:03 PM
    Friday, January 6, 2012 9:26 PM
  • Hi lipje;

    This is how it can be done. This defines the query but does not execute it at run time until the next line of code.

    Dim ToBeDeletedFoundOldLogFiles = _
        (From fl In My.Computer.FileSystem.GetFiles(Global_asax.ide.xmlfps)
          Where (DateTime.Now.Subtract(File.GetLastWriteTime(fl)).TotalDays > 5) _
         And Path.GetFileNameWithoutExtension(fl).StartsWith("rts485log created")
         Select fl))
    


    This following line will execute the above query and delete the files.

    ToBeDeletedFoundOldLogFiles.ToList().ForEach(Sub(f) Console.WriteLine(f))
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    Friday, January 6, 2012 9:30 PM

All replies

  • The way you have this written is actually what I would consider the "best".

     

    The issue here is that your For Each loop is intended to perform an action on each item.  Your LINQ query is exactly that - a "query".  Mixing the two means you create a query that has side effects, which is not necessarily a good idea.  See: http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx

     

    That being said, you can do this, via List(Of T).ForEach:

    (From fl In My.Computer.FileSystem.GetFiles(Global_asax.ide.xmlfps)
                Where (DateTime.Now.Subtract(File.GetLastWriteTime(fl)).TotalDays > 5) _
                And Path.GetFileNameWithoutExtension(fl).StartsWith("rts485log created")
                Select fl).ToList()
                .ForEach(Sub(file) FileIO.FileSystem.DeleteFile(file))
    


    However, as the article above demonstrates, what you have is likely a better, more maintainable option.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by lipje Friday, January 6, 2012 11:03 PM
    Friday, January 6, 2012 9:26 PM
  • Hi lipje;

    This is how it can be done. This defines the query but does not execute it at run time until the next line of code.

    Dim ToBeDeletedFoundOldLogFiles = _
        (From fl In My.Computer.FileSystem.GetFiles(Global_asax.ide.xmlfps)
          Where (DateTime.Now.Subtract(File.GetLastWriteTime(fl)).TotalDays > 5) _
         And Path.GetFileNameWithoutExtension(fl).StartsWith("rts485log created")
         Select fl))
    


    This following line will execute the above query and delete the files.

    ToBeDeletedFoundOldLogFiles.ToList().ForEach(Sub(f) Console.WriteLine(f))
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    Friday, January 6, 2012 9:30 PM
  • okay , i leave it this way for the time being. thank you for the answer. i thought of the sequence: from where select DO. you always do something with a query.
    nattelip
    Friday, January 6, 2012 11:10 PM