3

Linqで並べ替えようとしているローカルコレクションがありますが、返されたメモリコレクションは、数値ID列FailureIDで並べ替えられたままです。これらのOrderBy句が有効にならない理由はありますか?

物体

Public Class OpenBuildFaultsViewModel

    Public Property FailureID As Int64
    Public Property ModelName As String
    Public Property ZoneName As String
    Public Property Fault As String
    Public Property FaultCode As String
    Public Property FaultCodeDetail As String
    Public Property FaultArea As String
    Public Property MajorAssembly As String
    Public Property SubAssembly As String
    Public Property ComponentAssembly As String
    Public Property BusinessTest As String
    Public Property AuditScore As String
    Public Property Comment As String
    Public Property ShortagePart As String
    Public Property CreatedBy As String
    Public Property FixedByID As Int32
    Public Property FixedByComment As String
    Public Property FixedByFaultRectificationID As Int32

End Class

注文者

Function Index() As ActionResult

        Dim data As IEnumerable(Of OpenBuildFaultsViewModel) = Session("Failures")

        Dim model = From fails In data.Where(Function(w) w.FixedByID.Equals(0)).
                                        OrderBy(Function(o) o.MajorAssembly).
                                        OrderBy(Function(o) o.SubAssembly).
                                        OrderBy(Function(o) o.ComponentAssembly).
                                        OrderBy(Function(o) o.BusinessTest).
                                        OrderBy(Function(o) o.FailureID)

        Return View(model)

    End Function
4

1 に答える 1

7

OrderBy複数の基準でデータを並べ替える場合は、最初の基準にのみ使用する必要があります。次のものについては、次を使用する必要がありますThenBy

    Dim model = From fails In data.Where(Function(w) w.FixedByID.Equals(0)).
                                    OrderBy(Function(o) o.MajorAssembly).
                                    ThenBy(Function(o) o.SubAssembly).
                                    ThenBy(Function(o) o.ComponentAssembly).
                                    ThenBy(Function(o) o.BusinessTest).
                                    ThenBy(Function(o) o.FailureID)

OrderBy毎回使用すると、前の注文を「忘れて」、前の注文を無視して、次の基準で完全に並べ替えます。

ところで、そのFrom fails In部分は役に立たない。次のステートメントは同じ意味です。

    Dim model = data.Where(Function(w) w.FixedByID.Equals(0)).
                    OrderBy(Function(o) o.MajorAssembly).
                    ThenBy(Function(o) o.SubAssembly).
                    ThenBy(Function(o) o.ComponentAssembly).
                    ThenBy(Function(o) o.BusinessTest).
                    ThenBy(Function(o) o.FailureID)
于 2012-08-15T13:31:15.543 に答える