Results 1 to 8 of 8

Thread: [RESOLVED] Best way to find info NOT in a list using LINQ?

  1. #1

    Thread Starter
    Member din0's Avatar
    Join Date
    Aug 2011
    Location
    Indianapolis
    Posts
    34

    Resolved [RESOLVED] Best way to find info NOT in a list using LINQ?

    For a class project I am supposed to search through the attached file, and write to a new file the years between 1789 and 2009 that are not present in the fifth field. What is my best bet for determining what years are not present?

    So far I have this

    Code:
        Dim justices() As String = IO.File.ReadAllLines("Justices.txt")
    
        Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
            Dim queryAppointments = From line In justices
                                      Let year = line.Split(","c)(4)
                                      Select year
    
            Dim queryNoAppointments = From line In justices.Except(queryAppointments)
    
    
        End Sub
    End Class
    I'm kind of stuck after that. :/


    Should I make an array of integer between 1789 and 2009, then search for every year in the file, then set a flag to false if not found; is there a simpler way to accomplish this using LINQ?

    Thanks in advance for any help, I'm still shaky at best with my programming and fairly new.
    Attached Files Attached Files

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Best way to find info NOT in a list using LINQ?

    You can use the Enumerable.Range method to generate a sequence of numbers from 1789 and 2009. Call Except on that and pass the years from the file and you get the years in that range that aren't in the file, e.g.
    vb.net Code:
    1. Dim range = Enumerable.Range(1, 10)
    2. Dim numbers = {2, 6, 9}
    3. Dim missingNumbers = range.Except(numbers)
    4.  
    5. MessageBox.Show(String.Join(", ", missingNumbers))

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Best way to find info NOT in a list using LINQ?

    did that answer your question?

  4. #4

    Thread Starter
    Member din0's Avatar
    Join Date
    Aug 2011
    Location
    Indianapolis
    Posts
    34

    Re: Best way to find info NOT in a list using LINQ?

    I don't think I'm doing this right quite yet, but do I have the general idea down?
    Its throwing up some weird error on lines 9 and 12:
    Quote Originally Posted by Line 9 Error
    Error 1 Value of type 'Integer' cannot be converted to 'System.Collections.Generic.IEnumerable(Of Integer)'. C:\Users\DJ\documents\visual studio 2010\Projects\danbrockchp8ex32\danbrockchp8ex32\years.vb 9 39 danbrockchp8ex32
    Quote Originally Posted by Line 12 Error
    Error 2 Overload resolution failed because no accessible 'WriteAllLines' can be called with these arguments:
    'Public Shared Sub WriteAllLines(path As String, contents As System.Collections.Generic.IEnumerable(Of String))': Option Strict On disallows implicit conversions from 'Object' to 'System.Collections.Generic.IEnumerable(Of String)'.
    'Public Shared Sub WriteAllLines(path As String, contents() As String)': Option Strict On disallows implicit conversions from 'Object' to '1-dimensional array of String'. C:\Users\DJ\documents\visual studio 2010\Projects\danbrockchp8ex32\danbrockchp8ex32\years.vb 12 9 danbrockchp8ex32
    Code:
    Public Class Form1
        Dim justices() As String = IO.File.ReadAllLines("Justices.txt")
    
        Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
            Dim range = Enumerable.Range(1789, 2009)
    
            Dim query = From line In justices
                Let year = CInt(line.Split(","c)(4))
                Let noappt = range.Except(year)
                Select noappt
    
            IO.File.WriteAllLines("YearsNoAppt.txt", query)
        End Sub
    End Class

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Best way to find info NOT in a list using LINQ?

    here's how i'd write it:

    vb Code:
    1. Dim queryAppointments() As Integer = (From line In IO.File.ReadAllLines("Justices.txt") _
    2.                            Where Not line.Trim = "" _
    3.                            Let fields = line.Split(","c) _
    4.                            Select CInt(fields(4))).ToArray
    5.  
    6. Dim queryNoAppointments() As Integer = Enumerable.Range(1789, 221).Except(queryAppointments).ToArray

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Best way to find info NOT in a list using LINQ?

    or using strings instead of integers:

    vb Code:
    1. Dim queryAppointments() As String = (From line In IO.File.ReadAllLines("Justices.txt") _
    2.                            Where Not line.Trim = "" _
    3.                            Let fields = line.Split(","c) _
    4.                            Select fields(4)).ToArray
    5.  
    6. Dim queryNoAppointments() As String = Enumerable.Range(1789, 221).Select(Function(i) i.ToString).Except(queryAppointments).ToArray
    7. IO.File.WriteAllLines("filename", queryNoAppointments)

  7. #7

    Thread Starter
    Member din0's Avatar
    Join Date
    Aug 2011
    Location
    Indianapolis
    Posts
    34

    Re: Best way to find info NOT in a list using LINQ?

    Thanks to both of you guys that cleared up a lot for me; I think my favorite is the second example you listed, .paul., using strings. Seems a lot more concise than the given examples in the text book. Thanks again to both of you.

  8. #8

    Thread Starter
    Member din0's Avatar
    Join Date
    Aug 2011
    Location
    Indianapolis
    Posts
    34

    Re: [RESOLVED] Best way to find info NOT in a list using LINQ?

    You guys have helped me a lot lately and basically changed me from wondering if I can even learn to program to enjoying the things I'm learning on a daily basis!

Tags for this Thread

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