2011-05-03 26 views
1

我通常會在C#中執行此操作,但由於我必須在此特定程序集中獲得此代碼,因爲這是vb .net一,我卡住了。如何在Linq查詢中使用++的vb等效項來投射新類型

這裏是我的LINQ查詢:

Dim i As Integer = 0 

Dim oldAndCurrentIntersectionOnNames = From currentApplicant In currentApplicants _ 
            Group Join oldApplicant In oldApplicants _ 
             On _ 
             New With {Key .FirstName = currentApplicant.FirstName, _ 
                Key .LastName = currentApplicant.LastName} _ 
              Equals _ 
             New With {Key .FirstName = oldApplicant.FirstName, _ 
                Key .LastName = oldApplicant.LastName} Into applicants = Group _ 
            From applicant In applicants.DefaultIfEmpty(New ApplicantNameDetails()) _ 
            Select New ApplicantNameDetails() With _ 
            { _ 
             .Index = i+=1, _ 
             .FirstName = CStr(IIf(Not currentApplicant.FirstName Is Nothing, currentApplicant.FirstName, Nothing)), _ 
             .OldFirstName = CStr(IIf(Not applicant.FirstName Is Nothing, applicant.FirstName, Nothing)), _ 
             .LastName = CStr(IIf(Not currentApplicant.LastName Is Nothing, currentApplicant.LastName, Nothing)), _ 
             .OldLastName = CStr(IIf(Not applicant.LastName Is Nothing, applicant.LastName, Nothing)) _ 
            } 

你會看到的.index = I + = 1

這是我試圖做什麼,我想在C#中相當愉快地做(即指數=我在++)。不幸的是,VB編譯器不喜歡這樣。

有沒有人有任何建議,我怎麼會在VB中做到這一點。

在此先感謝

回答

1

Select方法存在過載問題,該方法允許您使用結果集合中項目的索引。 http://msdn.microsoft.com/en-us/library/bb534869.aspx

你可以分爲兩個部分查詢中使用它(未經測試)

Dim q = From currentApplicant In currentApplicants _ 
     Group Join oldApplicant In oldApplicants On _ 
        New With {Key.FirstName = currentApplicant.FirstName, _ 
          Key.LastName = currentApplicant.LastName} _ 
        Equals _ 
        New With {Key.FirstName = oldApplicant.FirstName, _ 
          Key.LastName = oldApplicant.LastName} Into applicants = Group _ 
     From applicant In applicants.DefaultIfEmpty(New ApplicantNameDetails()) 


Dim oldAndCurrentIntersectionOnNames = _ 
q.Select(Function(x, i) New ApplicantNameDetails() With _ 
     { _ 
      .Index = i, _ 
      .FirstName = CStr(IIf(Not x.currentApplicant.FirstName Is Nothing, x.currentApplicant.FirstName, Nothing)), _ 
      .OldFirstName = CStr(IIf(Not x.applicant.FirstName Is Nothing, x.applicant.FirstName, Nothing)), _ 
      .LastName = CStr(IIf(Not x.currentApplicant.LastName Is Nothing, x.currentApplicant.LastName, Nothing)), _ 
      .OldLastName = CStr(IIf(Not x.applicant.LastName Is Nothing, x.applicant.LastName, Nothing)) _ 
     })   
+0

Doh! '_必須......得到......它......要......工作......在......一個......單片......查詢_「談論獲得隧道視野並忽視分而治之!這很有用,非常感謝! (選擇缺少一個右括號btw) – Weevie 2011-05-04 08:39:04

3

本質上,你不能。如果您希望Linq查詢獲得連續的值,請使用一個特殊的(所謂的「生成器」)類,該類具有整數的IncrementAndGet(或簡單Next)方法。

class IntegerGenerator 
    private state as integer = 0 

    public function Next() as integer 
     dim oldState = state 
     state += 1 
     return oldState 
    end function 
end class 
+0

只是看了一下生成器類有關LINQ,我只看到他們有關的LINQ to SQL。這個查詢是一個LINQ到對象查詢,所以我想知道你的評論是否成立。你能在linq的背景下讓我知道爲什麼我不應該在C#中使用增量運算符在飛行中創建索引(或者你可以在VB中執行它) – Weevie 2011-05-03 15:52:39

+0

@Weevie對不起,混淆的術語。 Linq to SQL的上下文是完全不同的*通常*,「生成器」就是任何可以生成連續值的構造,例如一個隨機數生成器 – 2011-05-03 15:56:53

+0

您可以詳細介紹一下爲什麼我不應該使用方法我已經成功地在C#中使用LINQ到幾個對象? – Weevie 2011-05-03 15:56:58

相關問題