我试图编写一个可以从父级产品的外部集合中连接内部标签集合的单个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 - 所以我试图消除数据的双/三处理。
感谢
格雷姆