如何使用 ++ 的 vb 等效项对于投影新类型的 Linq 查询中的索引属性

发布于 2024-11-04 16:20:28 字数 1818 浏览 0 评论 0原文

我通常会在 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 中做到这一点有任何建议。

提前致谢

I'd normally do this in C# but since I've got to get this code in this particular assembly which is a vb.net one, I'm stuck.

Here's my linq query:

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)) _
                                    }

You'll see the .Index = i+=1

This was my attempt to do what I'd quite happily do in C# (i.e. Index = i++) in VB. Unfortunately the VB compiler doesn't like that.

Has anybody got any suggestions as to how I'd do this in VB.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-11-11 16:20:28

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

Essentially, you can’t. If you want the Linq query to get consecutive values, use a special (so-called “generator”) class that has an IncrementAndGet (or simply Next) method for your integer.

class IntegerGenerator
    private state as integer = 0

    public function Next() as integer
        dim oldState = state
        state += 1
        return oldState
    end function
end class
酒几许 2024-11-11 16:20:28

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)) _
         })         

There is an overload of the Select method that lets you use the index of the item on the result collection. http://msdn.microsoft.com/en-us/library/bb534869.aspx

You could split your query in two parts to use it (untested)

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)) _
         })         
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文