如何在单个 LINQ 中获得两个不同的聚合?

发布于 2024-11-05 20:33:09 字数 1373 浏览 1 评论 0原文

我有这个对象的列表:

public class Customer
{
    public int Id { get; set; }
    public string EmailAddress { get; set; }
    public DateTime ServiceStartDate { get; set; }
    public DateTime? BillingStartDate { get; set; }
    public string Status { get; set; }
}

在准备制作在仪表板上显示的图表时,我尝试将此列表压缩为该对象的另一个列表:

public class DashboardCustomerConversions
{
    public string Month { get; set; }
    public int Trials { get; set; }
    public int Purchased { get; set; }
}

最终结果看起来像:

Month       Trials   Purchases
---------   ------   ---------
Dec 2010    390      250
Jan 2011    345      190
Feb 2011    576      340

我很难想出一个LINQ语句可以达到想要的最终结果。这个语句非常接近:

var list = from b in results
           group b by new { b.ServiceStartDate.Year, b.ServiceStartDate.Month } into g
           select new Test
                      {
                          Month = string.Format("{0} {1}", CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(g.Key.Month), g.Key.Year),
                          Trials = g.Count(),
                          Purchased = g.Count()
                      };

明显的问题是“Purchased = g.Count()”行,因为它只是重复试验结果。我想对 BillingStartDate.HasValue 为 true 的对象进行计数。

有没有办法重组 LINQ 来完成这项工作?

编辑:我更喜欢流畅的语法风格,但我无法使上述内容正常工作。任何变化的答案都会很棒。

I have a list of this object:

public class Customer
{
    public int Id { get; set; }
    public string EmailAddress { get; set; }
    public DateTime ServiceStartDate { get; set; }
    public DateTime? BillingStartDate { get; set; }
    public string Status { get; set; }
}

In preparation for making a chart displayed on a dashboard I am trying to condense this list into another list of this object:

public class DashboardCustomerConversions
{
    public string Month { get; set; }
    public int Trials { get; set; }
    public int Purchased { get; set; }
}

Where the end result looks something like:

Month       Trials   Purchases
---------   ------   ---------
Dec 2010    390      250
Jan 2011    345      190
Feb 2011    576      340

I am having a hard time coming up with a LINQ statement that can achieve the desired end result. This statement is very close:

var list = from b in results
           group b by new { b.ServiceStartDate.Year, b.ServiceStartDate.Month } into g
           select new Test
                      {
                          Month = string.Format("{0} {1}", CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(g.Key.Month), g.Key.Year),
                          Trials = g.Count(),
                          Purchased = g.Count()
                      };

The obvious problem in is the "Purchased = g.Count()" line in that it just repeats the Trials result. I would like to count objects where the BillingStartDate.HasValue is true.

Is there a way to restructure the LINQ to make this work?

Edit: I would prefer a fluent style of syntax but I was unable to get the above to work. Answer in any variation would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

故笙诉离歌 2024-11-12 20:33:09

您需要将一个条件传递给 Count 方法。

Purchased = g.Count(q => q.BillingStartDate.HasValue)

You need to pass a condition to the Count method.

Purchased = g.Count(q => q.BillingStartDate.HasValue)
天生の放荡 2024-11-12 20:33:09

所以 SLAks 找到了正确的解决方案。这里是用流利的语法编写的:

listOfCustomer.GroupBy(c => new { c.ServiceStartDate.Year, c.ServiceStartDate.Month })
              .Select(group => new DashboardCustomerConversions()
                                {
                                    Month = string.Format("{0} {1}", CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(group.Key.Month), group.Key.Year),
                                    Trials = group.Count(),
                                    Purchased = group.Count(c => c.BillingStartDate.HasValue)
                                });

So SLaks had the right solution. Here it is written in fluent syntax:

listOfCustomer.GroupBy(c => new { c.ServiceStartDate.Year, c.ServiceStartDate.Month })
              .Select(group => new DashboardCustomerConversions()
                                {
                                    Month = string.Format("{0} {1}", CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(group.Key.Month), group.Key.Year),
                                    Trials = group.Count(),
                                    Purchased = group.Count(c => c.BillingStartDate.HasValue)
                                });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文