LINQ group-by然后显示日期时间(dd mmm) [英] LINQ group-by and then display datetime for (dd mmm)

查看:197
本文介绍了LINQ group-by然后显示日期时间(dd mmm)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Linq按日期分组,但在文本中显示

I want Linq to group by date but display in text

这里是我的代码

var groups = _uow.Orders.GetAll()
            .Where(x => x.Created > baselineDate)
            .GroupBy(x => x.Created.ToString("yyyy-MM-dd"));

var orders = new
        {
            Day = groups.Select(g => g.Key).ToArray(),
            Total = groups.Select(g => g.Sum(t => t.Total)).ToArray(),
        };

结果是(不太好放入图表的标签)

the result is (not good to put in label of graph)

 {"Day": [2, 3, 4, 5], "Total": [9999.00, 9999.00, 9999.00, 9999.00] }

但是我想要这个(每月)

But i want this (Monthly)

"Day": ['Jan', Feb', 'Mar', 'Apr'], "Total": [9999.00, 9999.00, 9999.00, 9999.00] }

或每日

"Day": ['Jan 1', 'Jan 2', 'Jan 3', 'Jan 4'], "Total": [9999.00, 9999.00, 9999.00, 9999.00] }

请给我建议我可以玩的DateTime.ToString()。

Please advice me for DateTime.ToString() that i can play with.

谢谢大家。 p>

Thank you all.

推荐答案

感谢大家的帮助。
我找到答案。

Thank you all for helping. I found the answer.

添加

.AsEnumerable()

AND

 .GroupBy(x => x.Created.ToString("MMM d"));

这里是完整的代码

var groups = _uow.Orders.GetAll()
            .Where(x => x.Created > baselineDate)
            .AsEnumerable()
            .GroupBy(x => x.Created.ToString("MMM d"));

        var orders = new
        {
            Day = groups.Select(g => g.Key).ToArray(),
            Total = groups.Select(g => g.Sum(t => t.Total)).ToArray(),
        };

这篇关于LINQ group-by然后显示日期时间(dd mmm)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆