如何使用LINQ对数据透视表进行分组

时间:2013-11-13 17:15:08

标签: c# sql linq

我是LINQ的新手,但从我读过的内容中查找我正在寻找的东西就像GroupBy - Simple 3。问题是我不想按一个小对象进行分组,我想分组并通过这些组分隔一个数据透视表(或直接创建它的查询),如下所示:

Pivot Table

查询: (我正在使用Visual Studio 2012,查询MS Access2010数据库)

        try
        {
            l.Open();
            OleDbCommand cmd = l.CreateCommand();
            string transform = "TRANSFORM Max(Ultima_Calibracao) AS MaxUltimaCalib ";
            string select = "SELECT Codigo, B_DSC AS Artigo, Marca_ AS Marca, Modelo, N_Serie ";
            string from = "FROM APP_Equip_Ult_Prox_Calibracao ";
            string where = "WHERE YEAR(Ultima_Calibracao) = " + ano +
                " AND [Descrição Serviço] like '" + servico + "'   ";
            string groupby = "GROUP BY APP_Equip_Ult_Prox_Calibracao.Codigo, APP_Equip_Ult_Prox_Calibracao.B_DSC, " +
                "APP_Equip_Ult_Prox_Calibracao.Marca_, APP_Equip_Ult_Prox_Calibracao.Modelo, APP_Equip_Ult_Prox_Calibracao.N_Serie," +
                "Year(DateAdd('m',Val([frequência calibração]),[Ultima_Calibracao])) ";
            string pivot = "PIVOT Format(Ultima_Calibracao,'yyyy-mm') IN ( '" + ano + "-01','" + ano + "-02','" + ano + "-03','" + ano + "-04','" + ano + "-05','" +
                ano + "-06','" + ano + "-07','" + ano + "-08','" + ano + "-09','" + ano + "-10','" + ano + "-11','" + ano + "-12');";
            string query = transform + select + from + where + groupby + pivot;

            cmd = new OleDbCommand(query, l);

            dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            l.Close();

        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("DAO Exception: " + ex.Message);
            return null;
        }

试图更好地解释自己,我想转向这样的事情:

按部门分组的查询结果:


| ID |说明|型号|部门 - |

| 1 | Item1 ------- | Model1 | Department1 |

| 3 | Item3 ------- | Model1 | Department1 |

| 2 | Item2 ------- | Model1 | Department2 |

| 4 | Item4 ------- | Model1 | Department2 |

这样的事情:

部门1

| ID |说明|型号|

| 1 | Item1 ------- | Model1 |

| 3 | Item3 ------- | Model1 |

部门2

| ID |说明|型号|

| 2 | Item2 ------- | Model1 |

| 4 | Item4 ------- | Model1 |

1 个答案:

答案 0 :(得分:2)

我不是100%肯定我会跟随,但这是我尝试回答的问题。在这里,我在DataTable中构建一些输入数据(这模仿了从查询中获得的内容),并使用一些LINQ将数据转换为新的DataSet。这是否回答了你的问题?

// create input data
var inputData = new DataTable();
inputData.Columns.Add("ID", typeof (int));
inputData.Columns.Add("Description", typeof (string));
inputData.Columns.Add("Model", typeof (string));
inputData.Columns.Add("Department", typeof (string));
inputData.Rows.Add(1, "Item1", "Model1", "Department1");
inputData.Rows.Add(2, "Item2", "Model1", "Department2");
inputData.Rows.Add(3, "Item3", "Model1", "Department1");
inputData.Rows.Add(4, "Item4", "Model1", "Department2");
inputData.AcceptChanges();

// create output data
var outputData = new DataSet();
outputData.Tables.AddRange(
    inputData.AsEnumerable()
             .GroupBy(row => row.Field<string>("Department"))
             .Select(rowGroup => {
                                     var departmentTable = inputData.Clone();
                                     departmentTable.TableName = rowGroup.Key;
                                     foreach (var row in rowGroup)
                                         departmentTable.Rows.Add(row.ItemArray);
                                     return departmentTable;
                                 })
             .ToArray());
outputData.AcceptChanges();