有效的数据结构来保持员工的活动? [英] Efficient data structure to hold employee's activities?

查看:132
本文介绍了有效的数据结构来保持员工的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个月内每天存储员工活动的目录中有n个Excel文件。他们有三列:日期,活动和类别。我不需要这个类别。

I have n excel files in a directory that stores employees' activities per day in a month. They have three columns: date, activity and category. I don't need the category.

我想基本上读取n excel文件,并输出一个word文档,有效地把每个员工按照日期排序的所有活动,例如:

I want to basically read the n excel files and output a word document that effectively puts all activities per employee ordered by date, for example:

第1天到第5天:

第一天:

员工#1 :

-Task a

-Task b

-Task c

Day one:
Employee #1:
-Task a
-Task b
-Task c

员工#2:

-Task a

-Task b

-Task c

...

Employee #2:
-Task a
-Task b
-Task c
...

第2天:
...

...

...

第7天至第11天:

...
...

Day 2: ...
...
...
Day 7 to day 11:
... ...

我想知道我可以用什么数据结构有效地保存这些信息,以便我可以轻松地写我想要的文件现在,我正在使用一个字符串数组来保存每个excel行,并将它们全部存储在List中,然后我将其存储在每个员工的字典中,密钥是每个员工的用户名。

I want to know what data structure i could use to effectively hold this information so i can easily write the document i want. Right now, i am using an array of string to hold each excel line, and store all of them in a List, which then i store in a dictionary for each employee, the key being the username of each employee.

虽然我认为这些数据结构本身是有效的,但是对于我的主要目标来说,它们不是太友善,这是打印每个员工每天订购的数据,所以关键本身应该是日期。

While i think these data structures are efficient themselves, they aren't too friendly for my main objective which is to print the data ordered per day for each employee, so maybe the key itself should be the date.

总结:正在使用的当前数据结构:

To recap: current data structure being used:

Dictionary<string,List<string[]>> dictActividades = new     Dictionary<string,List<string[]>>();

每个员工的excel文件中的所有行都存储在列表中。
我真的不需要字典功能,因为我可以按顺序阅读所有内容。一旦我正在阅读员工的excel文件,我可以打印,但我必须一次读取n个Excel文件(尽管n非常小)。

All lines in each employee's excel file are stored in a List. I really don't need the dictionary features because i could be reading everything back in order. I could be printing as soon as i am reading a employee's excel file but i would have to read the n excel files at once (although n is very small)

想法?

编辑:这是我目前拥有的:

here's what i currently have:

        string directorioActividades = @"\\mar-fp01\mar_tecnologia$\Coordinacion de Apoyo a Usuarios\Informes\" + 
            fechaInicio.Year.ToString() + "\\" + fechaInicio.Year.ToString() + "-" + 
            fechaInicio.Month.ToString().PadLeft(2, '0');

        string[] archivos = Directory.GetFiles(directorioActividades, "*.xlsx");
        Dictionary<string,List<string[]>> dictActividades = new Dictionary<string,List<string[]>>();
        for (int j = 0; j < archivos.Length; j++)
        {
            List<string[]> actividades = new List<string[]>();
            string nombreArchivo = Path.GetFileNameWithoutExtension(archivos[j]);
            String excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=" + archivos[j] + ";" +
                "Extended Properties=Excel 8.0; ";
            using (OleDbConnection con = new OleDbConnection(excelConnectionString))
            {
                OleDbCommand command = new OleDbCommand("Select * From [Actividades$]", con);
                con.Open();

                OleDbDataReader dr = command.ExecuteReader();
                int cantidadcolumnas = dr.FieldCount;

                string tipodatos = null;
                string[] filaDatos = new string[cantidadcolumnas];
                while (dr.Read())
                {
                    for (int k = 0; k < cantidadcolumnas; k++)
                    {
                        tipodatos = dr.GetFieldType(k).ToString();
                        if (tipodatos == "System.Int32")
                        {
                            filaDatos[k] = dr.GetInt32(k).ToString();
                        }
                        if (tipodatos == "System.String")
                        {
                            filaDatos[k] = dr.GetString(k);
                        }
                        if (tipodatos == "System.DateTime")
                        {
                            filaDatos[k] = dr.GetDateTime(k).ToShortDateString();
                        }
                    }
                    actividades.Add(filaDatos);
                }//while dr.read
            }
            dictActividades.Add(nombreArchivo, actividades);
        }//for archivos

虽然这段代码相当短,并且使用最少的数据结构我可以想到,打印是相当困难的,因为关键是员工的用户名,而不是日期,代码应该按每个员工的日期打印每个活动,以上面的格式示例

While this code is quite short and uses the fewest data structures i can think of, printing is quite difficult because the key is the employee's username and not the date and the code should print every activity by every employee by date as the example in the format posted above

推荐答案

我会建议一个类似这样的简单类

I would suggest a simple class like this

class EmployeeActivity
{
    public string Employee { get; set; }
    public DateTime Date { get; set; }
    public string Activity { get; set; }
}

没有特殊的数据结构 - 只是列表< EmployeeActivity> 。一旦填充,您可以使用LINQ来执行所需的排序/分组。

an no special data structure - just List<EmployeeActivity>. Once populated, you can use LINQ to perform ordering/grouping needed.

想像一下,您的字典不会填充我建议的内容

Imagine instead of you dictionary you populate what I'm suggesting

var empoyeeActivies = new List<EmployeeActivity>();
// Iterate excel files like in your code and populate the list

现在你可以转换您的示例中显示的方式与

Now you can transform it to the way shown in your example with

var result = employeeActivities
    .GroupBy(a => a.Date, (date, dateActivities) => new
    {
        Date = date,
        DateActivities = dateActivities
            .GroupBy(a => a.EmployeeName, (employeeName, employeeActivities) => new
            {
                EmployeeName = employeeName,
                Activities = empoyeeActivities.OrderBy(a => a.Activity)
            })
            .OrderBy(a => a.EmployeeName)
    })       
    .OrderBy(a => a.Date);

这篇关于有效的数据结构来保持员工的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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