Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于时间戳的对象组列表_C#_Linq_Asp.net Core Webapi - Fatal编程技术网

C# 基于时间戳的对象组列表

C# 基于时间戳的对象组列表,c#,linq,asp.net-core-webapi,C#,Linq,Asp.net Core Webapi,具有以下接口: public class foo { [Key] public int Id { get; set; }} public string Notes { get; set; } public DateTime? Timestamp { get; set; } } 使用数据上下文,例如:await context.foos.ToListAsync()返回完整的条目列表。我想要实现的目标是从如下相同的响应中重构JSON对象: [ {

具有以下接口:

public class foo
{
    [Key]
    public int Id { get; set; }}
    public string Notes { get; set; }
    public DateTime? Timestamp { get; set; }
}
使用数据上下文,例如:
await context.foos.ToListAsync()
返回完整的条目列表。我想要实现的目标是从如下相同的响应中重构JSON对象:

[
    {
        year: YYYY,
        count: 0, // <- total `foos` in year
        months: [
            {
                month: 'January',
                count: 0, // <- total `foos` in month
                days: [
                    {
                        day: 0,
                        count: 0, // <- total `foos` in day
                        foos: [
                            {
                                Id: 0,
                                Notes: '...',
                                Timestamp: '...'
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
[
{
年份:YYYY,

count:0,//算法上,这与您使用的语言无关,您可以按照以下伪代码执行操作:

for (item in listOfItems) {
    year = getYearFromItem(item)
    month = getMonthFromItem(item)
    day = gteDayFromItem(item)

    updateYears(data, year)
    updateMonth(data, month)
    updateDay(data, day)
}
您只需检查一次数据,并在执行过程中创建
年/月/日
子项目

有没有一种可能的方法可以通过LINQ实现这一点

可以使用适当的键对记录进行分组

//entries => List<foo>

var result = entries
    .GroupBy(_ => _.Timestamp?.Year) //Group all foo by year
    .Select(years => new { 
        year = years.Key, //YYYY
        count = years.Count(), // total `foos` in year
        months = years
            .GroupBy(_ => _.Timestamp?.Month) //Group all foo in this group by month
            .Select(months => new {
                month = months.First().Timestamp?.ToString("MMMM"), //January
                count = months.Count(),  // total `foos` in month
                days = months
                    .GroupBy(_ => _.Timestamp?.Day) //group all foo in this group by day
                    .Select(days => new {
                        day = days.Key,
                        count = days.Count(), // total `foos` in day
                        foos = days.ToArray()
                    }).ToArray()
            }).ToArray()
    });
//条目=>列表
var结果=条目
.GroupBy(=>u.Timestamp?.Year)//按年份对所有foo进行分组
.选择(年份=>新建{
year=years.Key,//YYYY
count=years.count(),//以年为单位的'foos'总数
月=年
.GroupBy(=>u.Timestamp?.Month)//按月将此组中的所有foo分组
.选择(月份=>新建){
month=months.First().Timestamp?.ToString(“MMMM”),//一月
count=months.count(),//月内的'foos'总数
天=月
.GroupBy(=>u.Timestamp?.Day)//按天将此组中的所有foo分组
.选择(天=>新建){
天=天。键,
count=days.count(),//总计'foos',以天为单位
foos=days.ToArray()
}).ToArray()
}).ToArray()
});
参考文献


参考

谢谢,我已经定制了这个,以满足我的具体需求。你能详细说明一下这个功能是如何工作的吗?或者为我指出正确的方向,以便更好地理解逻辑…@user1027620检查包含的链接