0

我是 LINQ 的新手,但从我阅读和查找的内容来看,我正在寻找类似GroupBy - Simple 3的内容。问题是我不想按一个小对象进行分组,我想按这些组进行分组并按这些组分隔一个数据透视表(或直接生成它的查询),如下所示:

数据透视表

查询:(我正在使用 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;
        }

为了更好地解释自己,我想变成这样:

查询结果按部门分组:


| 身份证 | 说明| 型号 | 部门-- |

| 1 | 项目1 --------| 模型1 | 部门1|

| 3 | 项目3 --------| 模型1 | 部门1|

| 2 | 项目2 --------| 模型1 | 部门2|

| 4 | 项目4 --------| 模型1 | 部门2|

变成这样:

部门 1

| 身份证 | 说明| 型号 |

| 1 | 项目1 --------| 模型1 |

| 3 | 项目3 --------| 模型1 |

部门 2

| 身份证 | 说明| 型号 |

| 2 | 项目2 --------| 模型1 |

| 4 | 项目4 --------| 模型1 |

4

1 回答 1

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();
于 2013-11-13T17:37:37.583 回答