Results 1 to 6 of 6

Thread: Lambda code not working

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2013
    Posts
    41

    Lambda code not working

    I'm trying to look through a directory and pick every file that doesn't have a string at the beginning, and I am having a go at doing this with lambdas, but I am getting it wrong somewhere

    Code:
    Dim di As DirectoryInfo = New DirectoryInfo(My.Settings.str_TargetDir)
    Dim fi_AllCSVfiles As FileInfo() = di.GetFiles("*.*", SearchOption.TopDirectoryOnly).Where(Function(x) (x.Name.StartsWith("ENCRYPTED_") = False))
    There may be multiple errors in here that are causing me problems, can anyone point them out to me?

    cheers!

  2. #2

    Thread Starter
    Member
    Join Date
    Oct 2013
    Posts
    41

    Re: Lambda code not working

    damn I feel stupid, I just forgot .toarray at the end of the getfiles line.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: Lambda code not working

    Wait... doesn't GetFiles return an array? It does ... In fact, it's an array of FileInfo... so... why would you need the .ToArray on the end of that? *scratches head*
    But, I guess if it works...


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Lambda code not working

    Quote Originally Posted by techgnome View Post
    Wait... doesn't GetFiles return an array? It does ... In fact, it's an array of FileInfo... so... why would you need the .ToArray on the end of that? *scratches head*
    But, I guess if it works...


    -tg
    You are correct but he pipes that into a linq where clause which will return an IEnumerable(Of T).
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  5. #5
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: Lambda code not working

    Oooooh.... ok... he meant a .ToArry at the VERY END.... gotcha... I thought he had done it in the middle... I'm with ya now.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Lambda code not working

    If you're going to use LINQ why not use EnumerateFiles? Can't imagine any performance difference but seems to fit better.

    vb Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Foo()
    6.         Dim di As New DirectoryInfo("path")
    7.         Dim csvFiles = di.EnumerateFiles("path").Where(Function(fi) Not fi.Name.StartsWith("ENCRYPTED_"))
    8.  
    9.     End Sub
    10. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width