ठीक है, यह काफी एक सवाल उत्तेजक माध्यम से है। मैंने अतीत में बहुत सारे डेटा फ़्लैटनिंग किए हैं, और आम तौर पर मैं सभी अद्वितीय मूल्यों को रखने के लिए एक शब्दकोश का उपयोग करता हूं, फिर बाद में उनसे शादी करता हूं।
आप LINQ के लिए कहा, अब मैं एक भी पास तरीका यह है के बारे में सोच नहीं कर सकते, तो मैं VB में बजाय इस मिल गया है, ...
Private Class FlatObj
Public Property G1_ID As Integer
Public Property G1_CellName As String
Public Property G2_ContainerID As Integer
Public Property G2_ID As Integer
Public Property G2_SerialNumber As Integer
Public Property G3_ID As Integer
Public Property G3_PrinterName As String
End Class
Private Class G1
Public Property ID As Integer
Public Property CellName As String
Public Property Containers As New List(Of G2)()
Public Property PrinterNames As New List(Of G3)()
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Return ID.Equals(CType(obj, G1).ID)
End Function
Public Overrides Function GetHashCode() As Integer
Return ID.GetHashCode()
End Function
End Class
Private Class G2
Public Property fID As Integer
Public Property ContainerID As Integer
Public Property SerialNumber As Integer
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Return ContainerID.Equals(CType(obj, G2).ContainerID)
End Function
Public Overrides Function GetHashCode() As Integer
Return ContainerID.GetHashCode()
End Function
End Class
Private Class G3
Public Property fID As Integer
Public Property PrinterName As String
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Return PrinterName.Equals(CType(obj, G3).PrinterName)
End Function
Public Overrides Function GetHashCode() As Integer
Return PrinterName.GetHashCode()
End Function
End Class
Dim fromDb As New List(Of FlatObj) From
{
New FlatObj() With {.G1_ID = 1971, .G1_CellName = "Default Cell", .G2_ContainerID = 1935, .G2_ID = 1971, .G2_SerialNumber = 1101929, .G3_ID = 1971, .G3_PrinterName = "PBG-PrtEmulator1"},
New FlatObj() With {.G1_ID = 1971, .G1_CellName = "Default Cell", .G2_ContainerID = 1936, .G2_ID = 1971, .G2_SerialNumber = 1101930, .G3_ID = 1971, .G3_PrinterName = "PBG-PrtEmulator1"},
New FlatObj() With {.G1_ID = 1971, .G1_CellName = "Default Cell", .G2_ContainerID = 2189, .G2_ID = 1971, .G2_SerialNumber = 1102183, .G3_ID = 1971, .G3_PrinterName = "PBG-PrtEmulator1"}
}
Dim g1s = fromDb.Select(Function(x) New G1 With
{
.ID = x.G1_ID,
.CellName = x.G1_CellName
}).Distinct().ToList()
Dim g2s = fromDb.Select(Function(x) New G2 With
{
.fID = x.G2_ID,
.ContainerID = x.G2_ContainerID,
.SerialNumber = x.G2_SerialNumber
}).Distinct().ToLookup(Function(x) x.fID)
Dim g3s = fromDb.Select(Function(x) New G3 With
{
.fID = x.G3_ID,
.PrinterName = x.G3_PrinterName
}).Distinct().ToLookup(Function(x) x.fID)
g1s.ForEach(Sub(g)
g.Containers.AddRange(g2s(g.ID))
g.PrinterNames.AddRange(g3s(g.ID))
End Sub)
सूचना है कि काफी का एक बहुत काम अलग() और ToLookup() एक्सटेंशन के माध्यम से चला गया है। उम्मीद है कि यह मदद करता है, मैं देखना चाहता हूं कि कोई और "LINQy" तरीका है: डी
बस स्पष्ट करने के लिए ... आप एक पदानुक्रम चाहते हैं जो ऑब्जेक्ट की तरह कुछ दिखता है - सेल> सेल के साथ। कंटेनर, सेल। सीरियल नब्स, सेल। प्रिंटर नाम (संग्रह के रूप में)? – Tom
क्या यह लिंक एसक्यूएल, लिंक से ईएफ, लिंक्स ऑब्जेक्ट्स या कुछ और है? –
@ टॉम - मुझे एक पदानुक्रम चाहिए जो सेल -> सेल। कंटेनर और सेल -> प्रिंटरनाम जैसा दिखता है। सीरियल नंबर कॉलम कंटेनर की एक संपत्ति है। –