[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.
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.
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:
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
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
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.
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!