2012-03-31 13 views
0

退職する上院議員と完全な上院議員の1人の2つのファイルがあります。退任する上院議員を完全なリストから削除する必要があります。私はLINQを使用していますが、私はそれが期待されるように動作していません。ある質問を他の質問と比較しようとしていますが、退職していない現在のリストから上院議員を取り出し、それらを新しいファイルに追加するだけです。しかし、私が新しいファイルを開くと、引退した上院議員を削除していません。私のコードは事前に助けてくれてありがとうございます。Visual Basic LINQ

 ' Query for retired senators 
    Dim senateRetiered = From line In retieredSen 
         Let data = line.Split(","c) 
         Let retName = data(0) 
         Select retName 

    ' query length 
    Dim index As Integer = senateRetiered.ToArray.Length 

     ' Add one to our index we will remove it later in our next query 
    index += 1 

    ' Query for or 110th senate compare to our other query and only select senators that have not retiered 
    Dim senate110Query = From line In senate110 
         Let data = line.Split(","c) 
         Let name = data(0) 
         Let state = data(1) 
         Let party = data(2) 
         Where name <> senateRetiered(index - 1) 
         Select line 

     ' Write the remaining sentators to a new file 
    IO.File.WriteAllLines("sortedSentate.txt", senate110Query) 

答えて

1

私は本当にあなたの質問を理解してわからないんだけど、私は2番目のクエリは、より多くのことのようになるはずだと思う:

Dim senate110Query = From line In senate110 
        Let data = line.Split(","c) 
        Let name = data(0) 
        Let state = data(1) 
        Let party = data(2) 
        Where Not senateRetired.Any(Function(r) r.Name = name) 
        Select line 
+0

を行うことができます。この宣言で

Class Senator Public Name As String Public State As String Public Party As String Public Overrides Function Equals(ByVal obj As Object) As Boolean Dim other = TryCast(obj, Senator) If other Is Nothing Then Return False End If Return Name.Equals(other.Name) End Function Public Overrides Function ToString() As String Return String.Format("{0},{1},{2}", Name, State, Party) End Function End Class 

役に立つかもしれないこれは私がどこ文が間違っていた働きました。ありがとうございました! – amedeiros

+1

'Let state = data(1)'と 'Let party = data(2)'は使用されていないので、それらをドロップすることができます。また 'senateRetired'は文字列を返します。したがって、条件は' senateRetired.Any(Function(n)n = name) 'でなければなりません。 –

0

私はやや複雑で、代替ソリューションを持っています。しかし、それはクラスとして上院議員を生み出す。あなたが作成をファイルに加えて、上院議員といくつかの他の作業を行う必要がある場合、それはあなたがこの

Dim senateRetiered = From line In retieredSen _ 
Let data = line.Split(","c) _ 
Select New Senator() With {.Name = data(0)} 

Dim senate110Query = From line In senate110 _ 
Let data = line.Split(","c) _ 
Select New Senator() With {.Name = data(0), .State = data(1), .Party = data(2)} 

Dim nonRetired = senate110Query.Except(senateRetiered) 

Dim nonRetiredString = nonRetired _ 
.Select(Function(sen) sen.ToString()) 
'Note: I have to append a ".ToArray()" with .NET 3.5 

IO.File.WriteAllLines("sortedSentate.txt", nonRetiredString)