我試圖編寫一個可以從父級產品的外部集合中連接內部標籤集合的單個Linq查詢,並返回標籤集合一個統一的計數命令按計數遞減。Linq從外部集合的內部集合中列出具有count的標籤列表
這裏有一個工作的解決方案,我目前有和正在尋求幫助轉換爲單個Linq查詢。
Module Module1
Class Tag
Property Name As String
Property Count As Integer
End Class
Class Product
Property Name As String
Property tags As List(Of Tag)
End Class
Sub Main()
Dim Products As New List(Of Product) From {
{New Product With {.Name = "P1",
.tags = New List(Of Tag) From {{New Tag With {.Name = "T1"}},
{New Tag With {.Name = "T3"}},
{New Tag With {.Name = "T5"}}
}
}
},
{New Product With {.Name = "P2",
.tags = New List(Of Tag) From {{New Tag With {.Name = "T2"}},
{New Tag With {.Name = "T4"}},
{New Tag With {.Name = "T6"}}
}
}
},
{New Product With {.Name = "P3",
.tags = New List(Of Tag) From {{New Tag With {.Name = "T2"}},
{New Tag With {.Name = "T3"}},
{New Tag With {.Name = "T4"}}
}
}
},
{New Product With {.Name = "P4",
.tags = New List(Of Tag) From {{New Tag With {.Name = "T4"}},
{New Tag With {.Name = "T5"}},
{New Tag With {.Name = "T6"}},
{New Tag With {.Name = "T7"}},
{New Tag With {.Name = "T8"}}
}
}
}
}
Dim ReportingTags As New List(Of Tag)
'-- Start : Needs to be converted to pure Linq (if possible)------------------------------------------
Dim InterimTags As New List(Of String)
For Each p As Product In Products
Dim TmpTags As New List(Of String)
InterimTags.AddRange(TmpTags.Concat(From t In p.tags Select t.Name))
Next
ReportingTags.AddRange((From t In InterimTags
Group By name = t Into Count = Count()
Select New Tag With {.Name = name,
.Count = Count}).OrderByDescending(Function(t) t.Count))
'-- End ----------------------------------------------------------------------------------------------
For Each t As Tag In ReportingTags
Console.WriteLine("Tag: {0} - Count: {1}", t.Name, t.Count)
Next
Console.ReadLine()
End Sub
前端模塊
我將採取輸出,並將其轉換爲(的TagModel)一個的ObservableCollection - 所以我試圖消除數據的雙/三處理。
感謝
格雷姆