解决 从MySQL查询创建多层数据模型

madaxe2020

成员
已加入
2020年9月7日
留言内容
17
编程经验
5-10
我如何扩展linq查询以创建附加级别的分组数据。

谢谢

马达克斯

Linq Query:
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            List<SignInReport> SignInReports = new List<SignInReport>();
            SignInReports.Add(new SignInReport { UserName = "Bob", EventName = "SignIn", EventTime = new DateTime(2020,1,18,6,0,0) });
            SignInReports.Add(new SignInReport { UserName = "Bob", EventName = "SignOut", EventTime = new DateTime(2020, 1, 18, 10, 0, 0) });
            SignInReports.Add(new SignInReport { UserName = "Bob", EventName = "SignIn", EventTime = new DateTime(2020, 1, 18, 11, 30, 0) });
            SignInReports.Add(new SignInReport { UserName = "Bob", EventName = "SignOut", EventTime = new DateTime(2020, 1, 18, 16, 30, 0) });
            SignInReports.Add(new SignInReport { UserName = "Charlie", EventName = "SignIn", EventTime = new DateTime(2020, 1, 18, 6, 0, 0) });
            SignInReports.Add(new SignInReport { UserName = "Charlie", EventName = "SignOut", EventTime = new DateTime(2020, 1, 18, 15, 30, 0) });
            SignInReports.Add(new SignInReport { UserName = "Charlie", EventName = "SignIn", EventTime = new DateTime(2020, 1, 19, 6, 30, 0) });
            SignInReports.Add(new SignInReport { UserName = "Charlie", EventName = "SignOut", EventTime = new DateTime(2020, 1, 19, 17, 45, 0) });

            IEnumerable<SignInReportGrouping> SignInData = SignInReports.GroupBy(u => u.UserName)
                                                  .Select(group => new SignInReportGrouping {   UserName = group.Key,
                                                                                                SignInReports = group.ToList() })
                                                  .ToList();
        }
    }

    public class SignInReport
    {
        public string UserName { get; set; }
        public string EventName { get; set; }
        public DateTime EventTime { get; set; }
    }

    public class SignInReportGrouping
    {
        public string UserName { get; set; }
        public IEnumerable<SignInReportDay> SignInReportDay { get; set; }
    }

    public class SignInReportDay
    {
        public IEnumerable<SignInReport> SignInReports { get; set; }
    }
}
 

madaxe2020

成员
已加入
2020年9月7日
留言内容
17
编程经验
5-10
最终,我想将我的日期集作为单个对象,并通过分组为新类将其转换为更复杂的对象结构。

SignInData(根)
SignInReportGrouping(按用户分组)
SignInReportDay(按日期分组)



Bob
2020,1,18
{2020,1,18,6,0,0,登录}
{2020,1,18,10,0,0,注销}
{2020,1,18,11,30,0,登录}
{2020,1,18,16,30,0,登出}
Charlie
2020, 1, 18
{2020,1,18,6,0,0,登录}
{2020,1,18,15,0,0,注销}
2020, 1, 19
{2020,1,19,6,30,0,登录}
{2020,1,19,17,45,0,登出}
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,525
地点
悉尼,澳大利亚
编程经验
10+
我相信这就是您要寻找的:
C#:
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp3
{
    class Program
    {
        static void Main()
        {
            var signInReports = new List<SignInReport>
                                {
                                    new SignInReport
                                    {
                                        UserName = "Bob",
                                        EventName = "SignIn",
                                        EventTime = new DateTime(2020, 1, 18, 6, 0, 0)
                                    },
                                    new SignInReport
                                    {
                                        UserName = "Bob",
                                        EventName = "SignOut",
                                        EventTime = new DateTime(2020, 1, 18, 10, 0, 0)
                                    },
                                    new SignInReport
                                    {
                                        UserName = "Bob",
                                        EventName = "SignIn",
                                        EventTime = new DateTime(2020, 1, 18, 11, 30, 0)
                                    },
                                    new SignInReport
                                    {
                                        UserName = "Bob",
                                        EventName = "SignOut",
                                        EventTime = new DateTime(2020, 1, 18, 16, 30, 0)
                                    },
                                    new SignInReport
                                    {
                                        UserName = "Charlie",
                                        EventName = "SignIn",
                                        EventTime = new DateTime(2020, 1, 18, 6, 0, 0)
                                    },
                                    new SignInReport
                                    {
                                        UserName = "Charlie",
                                        EventName = "SignOut",
                                        EventTime = new DateTime(2020, 1, 18, 15, 30, 0)
                                    },
                                    new SignInReport
                                    {
                                        UserName = "Charlie",
                                        EventName = "SignIn",
                                        EventTime = new DateTime(2020, 1, 19, 6, 30, 0)
                                    },
                                    new SignInReport
                                    {
                                        UserName = "Charlie",
                                        EventName = "SignOut",
                                        EventTime = new DateTime(2020, 1, 19, 17, 45, 0)
                                    }
                                };

            var signInData = signInReports.GroupBy(u => u.UserName)
                                          .Select(group1 => new SignInReportGrouping
                                                            {
                                                                UserName = group1.Key,
                                                                SignInReportDays = group1.GroupBy(g1 => g1.EventTime.Date)
                                                                                         .Select(group2 => new SignInReportDay
                                                                                                           {
                                                                                                               EventDate = group2.Key,
                                                                                                               SignInReports = group2.ToList()
                                                                                                           })
                                                            })
                                          .ToList();

            foreach (var signInReportGrouping in signInData)
            {
                Console.WriteLine(signInReportGrouping.UserName);

                foreach (var signInReportDay in signInReportGrouping.SignInReportDays)
                {
                    Console.WriteLine(signInReportDay.EventDate.ToShortDateString());

                    foreach (var signInReport in signInReportDay.SignInReports)
                    {
                        Console.WriteLine($"{signInReport.EventName}: {signInReport.EventTime}");
                    }
                }
            }

            Console.ReadLine();
        }
    }

    public class SignInReport
    {
        public string UserName { get; set; }
        public string EventName { get; set; }
        public DateTime EventTime { get; set; }
    }

    public class SignInReportGrouping
    {
        public string UserName { get; set; }
        public IEnumerable<SignInReportDay> SignInReportDays { get; set; }
    }

    public class SignInReportDay
    {
        public DateTime EventDate { get; set; }
        public IEnumerable<SignInReport> SignInReports { get; set; }
    }
}
 

madaxe2020

成员
已加入
2020年9月7日
留言内容
17
编程经验
5-10
公共类SignInReport {公共字符串UserName {get;放; }公用字符串EventName {get;放; } public DateTime EventTime {get;放; }}公共类SignInReportGrouping {public string UserName {get;放;公用IEnumerable<SignInReportDay>SignInReportDays {get;放; }} public class SignInReportDay {public DateTime EventDate {get;放;公用IEnumerable<SignInReport>SignInReports {get;放; }}

那正是我想要的,非常感谢,

马达克斯
 
最佳 底部