我有这样的表结构。
ContinentTable => CountryTable => UserTable
用户有CountryId。国家有ContinentId。
我想查询所有大陆的每个用户数。
我是通过SQL实现的。但我想使用linq扩展方法,因为我正在使用EF Core。
这是我的SQL查询
SELECT Continent.Id, Continent.Name, COUNT(Usr.UserName) AS 'User Count'
FROM Continents Continent, Countries Country, Users Usr
WHERE Continent.Id = Country.ContinentId
AND Country.Id = Usr.CountryId
GROUP BY Continent.Id, Continent.Name
答案 0 :(得分:0)
如果这是您的对象模型:
Dim continents As Dictionary(Of String, Dictionary(Of String, List(Of String)))
continents = New Dictionary(Of String, Dictionary(Of String, List(Of String))) From
{
{"Europe", New Dictionary(Of String, List(Of String)) From
{
{"France", New List(Of String) From {"Antoine", "Serge"}},
{"Germany", New List(Of String) From {"Hans", "Wolfgang"}},
{"Spain", New List(Of String) From {"Pablo", "Pedro"}}
}
},
{"America", New Dictionary(Of String, List(Of String)) From
{
{"USA", New List(Of String) From {"Kevin", "Caleb"}},
{"Mexico", New List(Of String) From {"Gustavo", "Oscar"}},
{"Brazil", New List(Of String) From {"Alexandro"}}
}
}
}
然后这将是您的LINQ查询:
Dim result As IEnumerable(Of KeyValuePair(Of String, Integer)) = continents.Select(Of KeyValuePair(Of String, Integer))(Function(x As KeyValuePair(Of String, Dictionary(Of String, List(Of String)))) New KeyValuePair(Of String, Integer)(x.Key, x.Value.Select(Of Integer)(Function(y As KeyValuePair(Of String, List(Of String))) y.Value.Count).Sum))