使用 linq 和 sum 聚合列表

发布于 2024-10-08 06:21:52 字数 821 浏览 0 评论 0原文

我正在尝试合并一个正在序列化的可枚举列表我

有如下所示的数据:

Internet explorer    10
Internet explorer    15 
Mozille firefox      10

Internet explorer    25
Mozille firefox      10

我的类看起来像:

 public class BrowserVisits
 {
  public string BrowserName { get; set; }

  public int Visits { get; set; }
        }

当前的序列化可枚举列表(r)的查询看起来像:

var browserVisits = from r in reportData
      select new BrowserVisits
      {
       BrowserName = r.Dimensions.First(d => d.Key == Dimension.Browser).Value,
       Visits = int.Parse(r.Metrics.First(d => d.Key == Metric.Visits).Value)
      };

我该怎么办linq 中带有 sum 的 group by ?

我需要进行两次查询,还是可以进行一次查询。

如果这听起来含糊不清,我深表歉意,这是漫长的一天,但如果需要澄清,我非常乐意补充这一点

i am trying to consolidate an ienumerable list that i am serializing

i have data that looks like this:

Internet explorer    10
Internet explorer    15 
Mozille firefox      10

I was it to look like:

Internet explorer    25
Mozille firefox      10

my class looks like:

 public class BrowserVisits
 {
  public string BrowserName { get; set; }

  public int Visits { get; set; }
        }

my current query to serialize an ienumerable list (r) looks like:

var browserVisits = from r in reportData
      select new BrowserVisits
      {
       BrowserName = r.Dimensions.First(d => d.Key == Dimension.Browser).Value,
       Visits = int.Parse(r.Metrics.First(d => d.Key == Metric.Visits).Value)
      };

how do i go a group by with sum in linq?

do i need to go two queries, or can i do a single one.

apologies if this sounds vague, its been a long day, but am more than happy to add to this if it needs clarification

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

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

发布评论

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

评论(3

尘曦 2024-10-15 06:21:57

怎么样:

public static IEnumerable<BrowserVisits> SumGroups(
    IEnumerable<BrowserVisits> visits)
{
    return visits.GroupBy(
        i => i.BrowserName,
        (name, browsers) => new BrowserVisits() {
            BrowserName = name,
            Visits = browsers.Sum(i => i.Visits)
        });
}

这对我来说对你的样本数据有用。

What about:

public static IEnumerable<BrowserVisits> SumGroups(
    IEnumerable<BrowserVisits> visits)
{
    return visits.GroupBy(
        i => i.BrowserName,
        (name, browsers) => new BrowserVisits() {
            BrowserName = name,
            Visits = browsers.Sum(i => i.Visits)
        });
}

This works for me against your sample data.

物价感观 2024-10-15 06:21:57
var lstBrowserVisits = new List<BrowserVisits> 
            { 

                new BrowserVisits{BrowserName = "Internet explorer", Visits = 10},
                new BrowserVisits{BrowserName = "Internet explorer", Visits = 15},
                new BrowserVisits{BrowserName = "Mozille firefox", Visits = 10} 
            }; 

var res = (from browserVisit in lstBrowserVisits
           group browserVisit by browserVisit.BrowserName into g
           select new { BrowserName = g.Key, Visits = g.Sum(s => s.Visits) });
var lstBrowserVisits = new List<BrowserVisits> 
            { 

                new BrowserVisits{BrowserName = "Internet explorer", Visits = 10},
                new BrowserVisits{BrowserName = "Internet explorer", Visits = 15},
                new BrowserVisits{BrowserName = "Mozille firefox", Visits = 10} 
            }; 

var res = (from browserVisit in lstBrowserVisits
           group browserVisit by browserVisit.BrowserName into g
           select new { BrowserName = g.Key, Visits = g.Sum(s => s.Visits) });
森林迷了鹿 2024-10-15 06:21:57

名称             价值

互联网浏览器sp;    10
互联网浏览器     15
Mozille firefox          10

var sum= list.GroupBy(a => a.Name).Select(a => new { Total= a.Sum(b => b.value),Name= a.Key }).OrderByDescending(a => a.Total).ToList();

Name                     Value

Internet explorer      10
Internet explorer      15
Mozille firefox           10

var sum= list.GroupBy(a => a.Name).Select(a => new { Total= a.Sum(b => b.value),Name= a.Key }).OrderByDescending(a => a.Total).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文