Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have datatable in following format.

ID Intalment Amount Date

1 FirstInst 10000 12\04\2014

1 SecondInst 10000 13\04\2014

1 ThirdInst 5000 14\04\2014

2 FirstInst 10000 14\04\2014

2 SecondInst 7000 17\04\2014


I want something like this...

ID FirstInst SecondInst ThirdInst FirstInstDate SecondInstDate ThirdInstDate

1 10000 10000 5000 12\04\2014 13\04\2014 14\04\2014

2 10000 7000 null 14\04\2014 17\04\2014 null
Posted
Comments
Gopi Kishan Mariyala 18-Apr-14 3:31am    
I think it is better if you go with pivot function in sql. In C#, you have to read each datarow and datacolumn to get the result you need i think

1 solution

Try this;

C#
var dtTable1 = new DataTable("Table1");
           var dtTable2 = new DataTable("Table2");


           dtTable1.Columns.AddRange
               (
                   new DataColumn[]
                       {
                           new DataColumn("ID", typeof (int)),
                           new DataColumn("Intalment", typeof (string)),
                           new DataColumn("Amount", typeof (float)),
                           new DataColumn("Date", typeof (DateTime))
                       }
               );


           dtTable2.Columns.AddRange
               (
                   new DataColumn[]
                   {
                       new DataColumn("ID", typeof (int)),
                       new DataColumn("FirstInstAmount", typeof (float)),
                       new DataColumn("SecondInstAmount", typeof (float)),
                       new DataColumn("ThirdInstAmount", typeof (float)),
                       new DataColumn("FirstInstDate", typeof (DateTime)),
                       new DataColumn("SecondInstDate", typeof (DateTime)),
                       new DataColumn("ThirdInstDate", typeof (DateTime))
                   }
               );

           var rowCon = dtTable1.NewRow();
           rowCon["ID"] = 1;
           rowCon["Intalment"] = "FirstInst";
           rowCon["Amount"] = 10000;
           rowCon["Date"] = new DateTime(2014, 4, 12);
           dtTable1.Rows.Add(rowCon);

           rowCon = dtTable1.NewRow();
           rowCon["ID"] = 1;
           rowCon["Intalment"] = "SecondInst";
           rowCon["Amount"] = 10000;
           rowCon["Date"] = new DateTime(2014, 4, 13);
           dtTable1.Rows.Add(rowCon);

           rowCon = dtTable1.NewRow();
           rowCon["ID"] = 1;
           rowCon["Intalment"] = "ThirdInst";
           rowCon["Amount"] = 5000;
           rowCon["Date"] = new DateTime(2014, 4, 14);
           dtTable1.Rows.Add(rowCon);

           rowCon = dtTable1.NewRow();
           rowCon["ID"] = 2;
           rowCon["Intalment"] = "FirstInst";
           rowCon["Amount"] = 10000;
           rowCon["Date"] = new DateTime(2014, 4, 14);
           dtTable1.Rows.Add(rowCon);

           rowCon = dtTable1.NewRow();
           rowCon["ID"] = 2;
           rowCon["Intalment"] = "SecondInst";
           rowCon["Amount"] = 7000;
           rowCon["Date"] = new DateTime(2014, 4, 17);
           dtTable1.Rows.Add(rowCon);


           var aa = from r in dtTable1.AsEnumerable()
                    group r by r.Field<int>("ID") into g
                    select new
                    {
                        g.Key,
                        firstIntalAmount = g.Where(s => s.Field<string>("Intalment").Equals("FirstInst")).Select(t => t.Field<float>("Amount")).FirstOrDefault(),
                        secondIntalAmount = g.Where(s => s.Field<string>("Intalment").Equals("SecondInst")).Select(t => t.Field<float>("Amount")).FirstOrDefault(),
                        thirdIntalAmount = g.Where(s => s.Field<string>("Intalment").Equals("ThirdInst")).Select(t => t.Field<float>("Amount")).FirstOrDefault(),
                        firstIntalDate = g.Where(s => s.Field<string>("Intalment").Equals("FirstInst")).Select(t => t.Field<DateTime>("Date")).FirstOrDefault(),
                        secondIntalDate = g.Where(s => s.Field<string>("Intalment").Equals("SecondInst")).Select(t => t.Field<DateTime>("Date")).FirstOrDefault(),
                        thirdIntalDate = g.Where(s => s.Field<string>("Intalment").Equals("ThirdInst")).Select(t => t.Field<DateTime>("Date")).FirstOrDefault()
                    };

           foreach (var item in aa)
           {
               var row = dtTable2.NewRow();
               row["ID"] = item.Key;
               row["FirstInstAmount"] = item.firstIntalAmount;
               row["SecondInstAmount"] = item.secondIntalAmount;
               row["ThirdInstAmount"] = item.thirdIntalAmount;
               row["FirstInstDate"] = item.firstIntalDate;
               row["SecondInstDate"] = item.secondIntalDate;
               row["ThirdInstDate"] = item.thirdIntalDate;
               dtTable2.Rows.Add(row);
           }

C#

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900