linq line transfer column

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace ConvertToTable
{
class Program
{
static void Main(string[] args)
{
#region 添加一个表
DataTable _dt = new DataTable();
_dt.Columns.Add(new DataColumn("staff_id", typeof(int)) { DefaultValue = 0 }); //员工 id
_dt.Columns.Add(new DataColumn("staff_Name", typeof(string)) { DefaultValue = "1" }); //员工名字
_dt.Columns.Add(new DataColumn("staff_TiCheng", typeof(string)) { DefaultValue = "1" });//员工提成规则
_dt.Columns.Add(new DataColumn("staff_TiChengAmount", typeof(double)) { DefaultValue = 0 }); //提成钱数

_dt.Rows.Add (1, "Li", "zero commission", 60);
_dt.Rows.Add (1, "Li", "reservation commission", 70);
_dt.Rows.Add (2, "Sally", "zero commission", 500);
_dt.Rows.Add (2, "Sally", "reservation commission", 60);
_dt.Rows.Add (2, "Sally", "Order commission ", 800);
_dt.Rows.Add (. 3," Wang "," zero commission ", 30);
_dt.Rows.Add (. 3," Wang "," Order commission ", 900);
#endregion
/ / output original table
Console.WriteLine ( "original table:");
DisplayTable (_dt);
// output lines after transfection list
Console.WriteLine ( "after the conversion table:");
DisplayTable (ConvertToTable (_dt));
Console.ReadLine ();
}

#Region conversion table
static ConvertToTable the DataTable (the DataTable Source)
{
the DataTable the DataTable dt = new new ();
// first two columns is fixed plus
dt.Columns.Add ( "staff_id");
dt.Columns.Add ( "staff_Name");
// staff_TiCheng field to filter condition into the column of the row below FIG
var columns = (from x in source.Rows.Cast <DataRow> () select x [2] .ToString . ()) the Distinct ();
// put staff_TiCheng field as a new field is added into
the foreach (var Item in Columns) dt.Columns.Add (Item) .DefaultValue = 0;
// X [. 1] is a field staff_Name by staff_Name g is the packet information if the packet is not read g.Key went to check a packet clause linq group name
var = Data from X in source.Rows.Cast <the DataRow> ()
Group X by X [. 1] INTO g
{Key = g.Key.ToString new new SELECT (), the Items G} =;
. data.ToList () the ForEach (X =>
{
// here is a string array can also be used according to the individual need DataRow
string [] = new new String Array [dt.Columns.Count];
// Array [. 1] is stored name
Array [. 1] = x.Key;
// traversing the second row
for (int I = 2; I <dt.Columns.Count; I ++)
{
// Array [0] is staff_id
IF (Array [0] == null)
Array [0] = x.Items.ToList <the DataRow> ( ) [0] [ "staff_id"] the ToString ();.
// Array [0] = (Y in x.Items from
// WHERE Y [2] .ToString () == dt.Columns [I] .ToString ( )
// SELECT Y [0] .ToString ()) the SingleOrDefault ();.
// Array [I] is a variety of commission
Array [I] = (Y in x.Items from
WHERE Y [2] .ToString () = = dt.Columns [i] .ToString () // y [2] is equal to various commission table name in the name column
select y [3] .ToString () // y [3] we are looking for is a variety staff_TiChengAmount the amount of money the commission
) .SingleOrDefault ();
}
dt.Rows.Add (Array); // add to the table
});
return dt;
}
/// <Summary>
/// output table
/// </summary>
/// <param name="dt"></param>
static void DisplayTable(DataTable dt)
{
//输出列的标题
dt.Columns.Cast<DataColumn>().ToList().ForEach(x => Console.Write(x + "\t"));
Console.WriteLine();
//输出每行的信息
dt.Rows.Cast<DataRow>().ToList().ForEach(x =>
{
x.ItemArray.ToList().ForEach(y => Console.Write(y.ToString() + "\t\t"));
Console.WriteLine();
});
}

#endregion
}
}

Guess you like

Origin www.cnblogs.com/hn_lijia/p/11784837.html