DataTable操作
1.【将一个表中数据复制到另一个table中】:
static void Main(string[] args)
{
DataTable unionDataTable = new DataTable();
DataTable dt1 = new DataTable();
//1.为表指定列名
unionDataTable.Columns.Add("AccountNo", typeof(string));//单号
unionDataTable.Columns.Add("RoomNo", typeof(string));//房间号
unionDataTable.Columns.Add("GoodsName", typeof(string));//商品名称
unionDataTable.Columns.Add("GoodsCount", typeof(string));//商品数量
unionDataTable.Columns.Add("State", typeof(string));//服务单状态
unionDataTable.Columns.Add("OrderId", typeof(string));//服务订单号
//DataRow dr;
//复制表结构
dt1 = unionDataTable.Clone();
//2.为 unionDataTable赋初值
for (int i = 0; i <= 2; i++)
{
//dr = unionDataTable.NewRow();
//dr["AccountNo"] = Guid.NewGuid().ToString();
unionDataTable.Rows.Add();
unionDataTable.Rows[i][0] = "F1902150001";
unionDataTable.Rows[i][1] = "A206";
unionDataTable.Rows[i][2] = "送毛巾";
unionDataTable.Rows[i][3] = "2";
unionDataTable.Rows[i][4] = "处理中";
unionDataTable.Rows[i][5] = "201902150001A206";
}
DataTable t = unionDataTable;
dt1 = unionDataTable.Clone();
//3.将unionDataTable表结构数据复制到dt1表
object[] obj = new object[dt1.Columns.Count];
for (int j = 0; j < unionDataTable.Rows.Count; j++)
{
unionDataTable.Rows[j].ItemArray.CopyTo(obj, 0);
dt1.Rows.Add(obj);
}
//return unionDataTable;
}
2.【将一个表中数据分组并查询特定字段】:
ZeroMoneyList:是一个datatable
var hotelGroupData = ZeroMoneyList.AsEnumerable().GroupBy(d => d.Field<string>("HotelCode")).Select(g => new { g.Key, Data = g.Select(d => new { HotelCode = d.Field<string>("HotelCode"), AccountNo = d.Field<string>("AccountNo"), Name = d.Field<string>("Name") }) });
“fool me once,shame on you. fool me twice, shame on me.”,翻译过来的意思是“愚弄我一次,是你坏;愚弄我两次,是我蠢”。