VB.NET LINQ 组加入问题
好吧,我知道有很多关于这个问题的帖子,但我花了几个小时试图解决这个问题,但我的大脑完全失去了知觉。
这是
我有一个包含 3 个属性(数量、服务、名称)的对象列表,
我希望列表中包含所有名称和服务(有点交叉连接)以及相应行的分组数量。不清楚,我猜
数量 服务名称
3 Srv2 鲍勃
4 Srv2 保罗
2 Srv1 保罗
1 Srv2 Nick
我想要作为输出
所有服务以及所有名称和相应数量(如果没有则为 0)
Srv1 Paul 2
Srv1 鲍勃 0
Srv1 尼克 0
Srv2 保罗 4
Srv2 鲍勃 3
Srv2 Nick 1
这是我到目前为止得到的结果,但我什至没有得到预期的结果 我非常确定有一种非常简单的方法可以实现我想要的...我会欢迎一些帮助...
谢谢
Dim services = (From a In interventions Select New With {.Service = a.Service}).Distinct()
Dim months = (From b In interventions Select New With {.Month = b.DateHeureIntervention.Month}).Distinct()
'Dim query = (From s In services _
' From m In months _
' From i In interventions.Where(Function(x) x.Service = s.Service And x.DateHeureIntervention.Month = m.Month).DefaultIfEmpty(New Intervention()) _
' Select New ChartItem With {.Service = s.Service, _
' .Name = MonthName(m.Month), _
' .Quantite = i.Quantite}).ToList()
'Return (From q In query _
' Group By srv = q.Service, n = q.Name Into Group _
' Select New ChartItem With {.Name = n, _
' .Service = srv, _
' .Quantite = Group.Sum(Function(i) i.Quantite)}).ToList()
Dim q = (From i In interventions _
Group Join s In services On s.Service Equals i.Service Into si = _
Group Join m In months On m.Month Equals i.DateHeureIntervention.Month _
From x In si.DefaultIfEmpty() _
Group By m = i.DateHeureIntervention.Month, srv = i.Service Into Group _
Select New ChartItem With {.Service = srv, _
.Name = MonthName(m), _
.Quantite = Group.Sum(Function(y) y.i.Quantite)}).ToList()
Return q
Ok I know there is a lot of posts on this but I have trying to solve this for a few hours and my brain is completely off.
Here is the thing
I have a list of object with 3 properties (quantity, service, name)
I want to have all names and services in my list (kinda cross join) and the grouped quantity of the row corresponding. Not clear i guess
Quantity Service Name
3 Srv2 Bob
4 Srv2 Paul
2 Srv1 Paul
1 Srv2 Nick
I want as output
All the services and all the names and corresponding quantities (0 if none)
Srv1 Paul 2
Srv1 Bob 0
Srv1 Nick 0
Srv2 Paul 4
Srv2 Bob 3
Srv2 Nick 1
Here is what I got so far, but I dont even get the expected results
And I am acutally certain there is a pretty easy way of achieving what i want... I little help would be welcome...
Thanks
Dim services = (From a In interventions Select New With {.Service = a.Service}).Distinct()
Dim months = (From b In interventions Select New With {.Month = b.DateHeureIntervention.Month}).Distinct()
'Dim query = (From s In services _
' From m In months _
' From i In interventions.Where(Function(x) x.Service = s.Service And x.DateHeureIntervention.Month = m.Month).DefaultIfEmpty(New Intervention()) _
' Select New ChartItem With {.Service = s.Service, _
' .Name = MonthName(m.Month), _
' .Quantite = i.Quantite}).ToList()
'Return (From q In query _
' Group By srv = q.Service, n = q.Name Into Group _
' Select New ChartItem With {.Name = n, _
' .Service = srv, _
' .Quantite = Group.Sum(Function(i) i.Quantite)}).ToList()
Dim q = (From i In interventions _
Group Join s In services On s.Service Equals i.Service Into si = _
Group Join m In months On m.Month Equals i.DateHeureIntervention.Month _
From x In si.DefaultIfEmpty() _
Group By m = i.DateHeureIntervention.Month, srv = i.Service Into Group _
Select New ChartItem With {.Service = srv, _
.Name = MonthName(m), _
.Quantite = Group.Sum(Function(y) y.i.Quantite)}).ToList()
Return q
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我得到的是我的结果,肯定有更好的方法,但无论如何,它有效
I got it here is my result, there surely is a better way, but anyhow, it works