1

最終的にテーブルを作成するには、各セクションを繰り返し処理し、行のセルを作成する必要があります。ここで、各セクションには行0....nがあり、行にはセルがあります。0...n

たとえば、以下に示すように:

Row 0->   Cell0 | Cell1| Cell2 | Cell3 | Cell4 | Cell5         
Row 1->   Cell0 | Cell1| Cell2 | Cell3 | Cell4 | Cell5      
Row 2->   Cell0 | Cell1| Cell2 | Cell3 | Cell4 | Cell5     
..

ラムダ式を作成したい(たとえば):

var allColumns = ...........         

すべてのCell0、次にすべてのCell1、すべてのCell2などを取得して、ループで反復し、それぞれのテーブルを作成できるようにします。要件では、行ごとに移動する必要はありませんが、列ごとに移動する必要があります(上の図に示すように、セル0を最初に作成し、次にセル1を作成し、次にセル2を作成します。以下のループのように)。

foreach(Cell c in allColumns){
    //I want to iterate in this loop and create my cells and rows.    
}

このためのラムダ式を教えてください。

私はこれをやってみました

var allColumns = Sections.Rows.SelectMany(a=>a.Cells).GroupBy(What should come here).                             

上記のforeachループと上記の図を念頭に置いて、より良いLambda式を改善または提案できますか?前もって感謝します。

4

2 に答える 2

2

私がまとめたこのデモを試して、それが機能するかどうかを確認できます (Windows コンソール アプリケーションに配置する必要があります)。

        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { 
               new DataColumn("col1"), 
               new DataColumn("col2"), 
               new DataColumn("col3") });

        dt.Rows.Add("cell0", "cell1", "cell2");
        dt.Rows.Add("cell0", "cell1", "cell2");
        dt.Rows.Add("cell0", "cell1", "cell2");
        dt.Rows.Add("cell0", "cell1", "cell2");

        // writes out the original data
        foreach (DataRow row in dt.Rows)
        {
            foreach (var column in row.ItemArray)
                Console.Write("{0} ", column);

            Console.WriteLine();
        }
        Console.WriteLine();

        // reverses columns and rows
        var result = dt.Columns
                       .Cast<DataColumn>()
                       .Select(column => 
                          dt.AsEnumerable()
                            .Select(row => 
                               row.ItemArray[column.Ordinal].ToString()));

        // writes out the reversed columns and rows
        foreach (var row in result)
        {
            foreach(var column in row)
                Console.Write("{0} ",column);

            Console.WriteLine();
        }

        Console.ReadKey();
于 2012-06-05T11:04:15.243 に答える
0

Linqpadデモ プログラム

Ivan Gs コードを使用して、Linqpad プログラムを作成しました

DataTable の列を取得する

void Main(){        
    // get and write out the original data
    var dt = GetDataTable();
    dt.Dump();
    // get columns
    var result = GetColumnsFromDataTable(dt);
    result.ElementAt(0).Dump();
    result.ElementAt(1).Dump();
    result.ElementAt(2).Dump(); 
}
   
private IEnumerable<EnumerableRowCollection<String>> GetColumnsFromDataTable(DataTable dt){

    // Lambda Expression to get the cells 
    // vertically for a section containing rows     
    var result = dt.Columns.Cast<DataColumn>()
               .Select(column =>  dt.AsEnumerable()
               .Select(row => row.ItemArray[column.Ordinal].ToString()));   
    return result;
}

private DataTable GetDataTable(){
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[] { 
        new DataColumn("col1 id"), 
        new DataColumn("col2 name"), 
        new DataColumn("col3 job") 
    });
    dt.Rows.Add("1", "John", "Barista");
    dt.Rows.Add("2", "Mike", "Handyman");
    dt.Rows.Add("3", "Jane", "Teacher");
    dt.Rows.Add("4", "Quentin", "Producer");
    return dt;
}
于 2013-02-17T07:33:23.523 に答える