如何在投影新类型的Linq查询中使用相当于c++的vb索引属性



我通常会在c#中这样做,但由于我必须在这个特定的程序集中获得这个代码,这是一个vb.net,我卡住了。

这是我的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

这是我尝试在VB中做我很高兴在c#中做的事情(即Index = i++)。不幸的是,VB编译器不喜欢这样。

有没有人对我如何在VB中做到这一点有任何建议。

Thanks in advance

基本上,你不能。如果您希望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

有一个超载的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)) _
         })         

相关内容

  • 没有找到相关文章

最新更新