Lambda Expression to get the cells vertically for a section containing rows

大兔子大兔子 提交于 2019-12-11 09:35:04

问题


I have to iterate through each section and create the cells for rows to ultimately create a table. Here each Section has rows 0....n, and Rows have cells say 0...n

For example as illustrated below:

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

I want to create a lambda expression (say):

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

To get all Cell0's then all Cell1's, all Cell2's etc... so that I can iterate in loop and create a table for each. The requirement does not want me to go row by row but it wants me to go column by column (cell as illustrated above in the diagram, I create the the cell0's first then cell1's then cell 2's etc, like in the loop below.

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

Can you help give me the lambda expression for this.

I tried doing this

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

Can you improve or suggest a better Lambda expression keeping the above foreach loop and illustration above in mind. thanks in advance.


回答1:


You can try this demo which I had put together and see if it works for you (it should be put in a Windows console application):

        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();



回答2:


Linqpad Demo Program

I took Ivan Gs Code and created a Linqpad Program

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;
}


来源:https://stackoverflow.com/questions/10894660/lambda-expression-to-get-the-cells-vertically-for-a-section-containing-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!