Quantcast
Channel: Pivot table in c# entity framework
Viewing all articles
Browse latest Browse all 6

Pivot table in c# entity framework

$
0
0

Hi vicksss;

I am not sure what your classes Currency and Rate are in your code. The code snippet below makes a query to a table in the database I called ISO and the EDX tool renamed it to ISOes. The columns in that table is the same as what you call Table1. The function PopulateGrid takes the results of that query and creates a DataTable / Pivot table. See the comments in the snippet.

publicpartialclass Form1 : Form
{public Form1( )
  {
    InitializeComponent( );
  }privatevoid button1_Click( object sender, EventArgs e )
  {// The ObjectContext I created using EF
    ISOEntities ctx = new ISOEntities();// A Linq to EF which creates a List of concreate class called IsoPivot.
    List<IsoPivot> queryResults = 
      (from iso in ctx.ISOesorderby iso.ISOCode ascending , iso.Year ascendinggroup iso by iso.ISOCode into isoGroupselectnew IsoPivot()
            {
              ISOCode = isoGroup.Key,
              Description = isoGroup.Select(d => d.Description).FirstOrDefault(),
              Year = isoGroup.Select(y => y.Year),
              Value = isoGroup.Select(v => v.Value)
            }).ToList();// Call a function to create a dynamically created data table with the needed columns
    PopulateGrid(queryResults);
  }private DataTable dt;privatevoid PopulateGrid(List<IsoPivot> qr)
  {// Create a DataTable as a DataSource for the grid
    dt = new DataTable();// Create the DataColumns for the data table
    DataColumn dc = new DataColumn("ISO Code", typeof(string));
    dt.Columns.Add(dc);
    dc = new DataColumn( "Description", typeof( string ) );
    dt.Columns.Add( dc );// Get a list of Distinct yearsvar yearLabel = (from yList in qr.Select(year => year.Year)from year in yListselect year.ToString()).Distinct().ToList();// Create the DataColumns for the table
    yearLabel.ForEach( delegate( string year )
    {
      dc = new DataColumn( year, typeof( string ) );
      dt.Columns.Add( dc );        
    });// Populate the rowa of the DataTableforeach (IsoPivot rec in qr)
    {// The first two columns of the row always has a ISO Code and Description
      DataRow dr = dt.NewRow();
      dr[0] = rec.ISOCode;
      dr[1] = rec.Description;// For each recordvar years = rec.Year.ToList();var values = rec.Value.ToList();// Because each row may have different years I am indexing// the with the string namefor( int i = 0; i < values.Count; i++ )
      {
        dr[years[i].ToString()] = values[i];
      }// Add the DataRow to the DataTable
      dt.Rows.Add(dr);
    }// Bind the DataTable to the DataGridView
    dataGridView1.DataSource = dt;

  }

}

publicclass IsoPivot
{publicstring ISOCode { get; set; }publicstring Description { get; set; }public IEnumerable<int> Year { get; set; }public IEnumerable<decimal> Value { get; set; }
}

 


Fernando

If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>