I have done the job this way. here is two sample code.
1st Set of code
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("RowNumber", typeof(int));
dt.Columns.Add("Section", typeof(string));
dt.Columns.Add("LineItem", typeof(string));
dt.Columns.Add("DisplayInCSM", typeof(string));
dt.Columns.Add("Broker", typeof(string));
dt.Columns.Add("BrokerName", typeof(string));
dt.Columns.Add("ItemValue_NoFormat", typeof(int));
dt.Columns.Add("Period", typeof(string));
dt.Rows.Add(new object[] {1, "Operational", "NG Sales", "NGL", "CR", "Credit Suse", 200, "2010 FYA"});
dt.Rows.Add(new object[] {2, "Operational", "NG Sales", "NGL", "GR", "Max 1", 300, "2010 FYA"});
dt.Rows.Add(new object[] {3, "Operational", "NG Sales", "NGL", "PX", "Morgan", 100,"2010 FYA"});
dt.Rows.Add(new object[] {4, "Operational", "NG Sales", "NGL", "WB", "Wells Fargo", 500, "2010 FYA"});
string[] brokers = dt.AsEnumerable().Select(x => x.Field<string>("Broker")).Distinct().OrderBy(x => x).ToArray();
DataTable pivot = new DataTable();
pivot.Columns.Add("Section", typeof(string));
pivot.Columns.Add("LineItem", typeof(string));
pivot.Columns.Add("DisplayInCSM", typeof(string));
foreach (string broker in brokers)
{
pivot.Columns.Add(broker, typeof(int));
}
var groups = dt.AsEnumerable().GroupBy(x => new { section = x.Field<string>("Section"), lineItem = x.Field<string>("LineItem"), csm = x.Field<string>("DisplayInCSM")}).ToList();
foreach (var group in groups)
{
DataRow newRow = pivot.Rows.Add();
newRow["Section"] = group.Key.section;
newRow["LineItem"] = group.Key.lineItem;
newRow["DisplayInCSM"] = group.Key.csm;
foreach (DataRow row in group)
{
newRow[row.Field<string>("Broker")] = row.Field<int>("ItemValue_NoFormat");
}
}
}
}
}
2nd Set of code
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
List<CSM> csms = new List<CSM>() {
new CSM(1, "Operational", "NG Sales", "NGL", "CR", "Credit Suse", 200, "2010 FYA"),
new CSM(2, "Operational", "NG Sales", "NGL", "GR", "Max 1", 300, "2010 FYA"),
new CSM(3, "Operational", "NG Sales", "NGL", "PX", "Morgan", 100,"2010 FYA"),
new CSM(4, "Operational", "NG Sales", "NGL", "WB", "Wells Fargo", 500, "2010 FYA")
};
string[] brokers = csms.Select(x => x.Broker).Distinct().OrderBy(x => x).ToArray();
DataTable pivot = new DataTable();
pivot.Columns.Add("Section", typeof(string));
pivot.Columns.Add("LineItem", typeof(string));
pivot.Columns.Add("DisplayInCSM", typeof(string));
foreach (string broker in brokers)
{
pivot.Columns.Add(broker, typeof(int));
}
var groups = csms.GroupBy(x => new { section = x.Section, lineItem = x.LineNumber, csm = x.DisplayInCSM}).ToList();
foreach (var group in groups)
{
DataRow newRow = pivot.Rows.Add();
newRow["Section"] = group.Key.section;
newRow["LineItem"] = group.Key.lineItem;
newRow["DisplayInCSM"] = group.Key.csm;
foreach (CSM row in group)
{
newRow[row.Broker] = row.ItemValue_NoFormat;
}
}
}
}
public class CSM
{
public int RowNumber { get;set;}
public string LineNumber { get; set; }
public string Section { get; set; }
public string DisplayInCSM { get;set;}
public string Broker { get;set;}
public string BrokerName { get;set;}
public int ItemValue_NoFormat { get;set;}
public string Period{ get;set;}
public CSM() {}
public CSM(
int RowNumber,
string LineNumber,
string Section,
string DisplayInCSM,
string Broker,
string BrokerName,
int ItemValue_NoFormat,
string Period)
{
this.RowNumber = RowNumber;
this.LineNumber = LineNumber;
this.Section = Section;
this.DisplayInCSM = DisplayInCSM;
this.Broker = Broker;
this.BrokerName = BrokerName;
this.ItemValue_NoFormat = ItemValue_NoFormat;
this.Period = Period;
}
}
}