嵌套的字典:按日期组,然后按组枚举值和汇总计数

我的原始数据是这样的:嵌套的字典:按日期组,然后按组枚举值和汇总计数

IDictionary<DateTime, IDictionary<ReportType, int>> rawData 

例子:

19/09/2017, [(ReportType.Open, 2)] 

25/09/2017, [(ReportType.Open, 15), (ReportType.Sent, 15)]

25/10/2017, [(ReportType.Open, 27), (ReportType.Sent, 15)]

29/10/2017, [(ReportType.Sent, 150)]

我想达到以下结果:

data for open: 

september-2017: 17

october-2017: 27

data for sent:

september-2017: 15

october-2017: 165

-----------------------------

我试图

var openData = rawData 

.Select(g => new {

dt = string.Format("{0}/{1}", g.Key.Month, g.Key.Year),

g.Key.Year,

g.Key.Month,

action = g.Value.FirstOrDefault().Key,

total = g.Value.Values.Sum()

})

.GroupBy(l => new {

l.Month,

l.Year,

action = l.action

})

.Where(a => a.Key.action == ReportType.Open)

.OrderByDescending(a => a.Key)

.ToList();

我在做什么错?

打印部(series1Series类派生):

foreach (var item in openData) 

{

series1.Points.Add(new DataPoint()

{

YValues = new double[] { item.total },

AxisLabel = item.dt.ToString(),

ToolTip = item.total.ToString()

});

}

回答:

您正在过滤Open reporttype错误的地方;

  var openData = rawData 

.Select(g => new {

dt = string.Format("{0}/{1}", g.Key.Month, g.Key.Year),

g.Key.Year,

g.Key.Month,

actions = g.Value.Where(x => x.Key == ReportType.Open) //Filter it here

})

.GroupBy(l => new {

l.Month,

l.Year

})

.Select(s =>

new

{

s.Key.Month,

s.Key.Year,

s.Sum(x => x.actions.Sum(sum => sum.Value))

})

.ToList();

回答:

有时候,一个简单的嵌套foreach仍然可以得到最好的&清晰的解决方案。在大多数情况下,大数据集会比LINQ解决方案更快。

Dictionary<string, int> openCounts = new Dictionary<string, int>(); 

Dictionary<string, int> sentCounts = new Dictionary<string, int>();

foreach (var kvp in rawData)

{

//var dt = string.Format("{0}-{1}", kvp.Key.ToString("MMMM"), kvp.Key.Year);

var dt = kvp.Key.ToString("MMMM-yyyy"); //simpler alternative, "MMMM" gives full month name

foreach (var val in kvp.Value)

{

if (val.Key == ReportType.Open)

{

if (openCounts.ContainsKey(dt))

openCounts[dt] += val.Value;

else

openCounts.Add(dt, val.Value);

}

else

{

if (sentCounts.ContainsKey(dt))

sentCounts[dt] += val.Value;

else

sentCounts.Add(dt, val.Value);

}

}

}

回答:

在您的查询的第一部分,你正在服用的集合中唯一的第一个项目。那么你正在分组哪些是错误的。 尝试如下:

 var ob = (from m in rawData 

where m.Value.Keys.Any(i => i != ReportType.Sent)

group m by new { m.Key.Year, m.Key.Month, m.Value.Keys } into gx

select new

{

Date = new DateTime(gx.Key.Year, gx.Key.Month, 1),

// taa = gx.Key.Keys,

Total = gx.Select(m => m.Value.Values).ToList().Select(y => y.Sum()).FirstOrDefault()

}).ToList();

回答:

我用下面的代码来实现您所期望的:

var result = rawData 

.SelectMany(c => c.Value, (b, c) => new { b.Key.Year, Month = b.Key.ToString("MMMM"), c.Key, c.Value })

// At first I unwind nested data

.GroupBy(g => g.Key)

// Then create my new group by ReportType

.Select(c => new {

c.Key,

Data = c.GroupBy(g => new { g.Year, g.Month })

.Select(f => new { f.Key.Year, f.Key.Month, Sum = f.Sum(s => s.Value) })

// Each ReportType has many groups of monthes of each year

})

// At last create my custom type

.ToList();

C# Fiddle Demo

以上是 嵌套的字典:按日期组,然后按组枚举值和汇总计数 的全部内容, 来源链接: utcz.com/qa/261157.html

回到顶部