VB.NET LINQ 组加入问题

发布于 2024-10-06 08:54:10 字数 2020 浏览 2 评论 0原文

好吧,我知道有很多关于这个问题的帖子,但我花了几个小时试图解决这个问题,但我的大脑完全失去了知觉。

这是

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除→记忆 2024-10-13 08:54:10

我得到的是我的结果,肯定有更好的方法,但无论如何,它有效

    Dim months As List(Of Integer) = (From b In interventions Select b.DateHeureIntervention.Month).Distinct().ToList()
    Dim services As List(Of String) = (From c In interventions Select c.Service).Distinct().ToList()

    Dim q = (From s In services _
             From m In months _
             Select New ChartItem With {.Name = MonthName(m), _
                                        .Service = s, _
                                        .Quantite = (From i In AccesDonneesHelper.GetInterventionsDetails() _
                                                     Where i.Service = s And i.DateHeureIntervention.Month = m _
                                                     Select i.Quantite).Sum()}).ToList()

I got it here is my result, there surely is a better way, but anyhow, it works

    Dim months As List(Of Integer) = (From b In interventions Select b.DateHeureIntervention.Month).Distinct().ToList()
    Dim services As List(Of String) = (From c In interventions Select c.Service).Distinct().ToList()

    Dim q = (From s In services _
             From m In months _
             Select New ChartItem With {.Name = MonthName(m), _
                                        .Service = s, _
                                        .Quantite = (From i In AccesDonneesHelper.GetInterventionsDetails() _
                                                     Where i.Service = s And i.DateHeureIntervention.Month = m _
                                                     Select i.Quantite).Sum()}).ToList()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文