日期:2014-05-20 浏览次数:20784 次
void Main() { DataTable dt=new DataTable(); dt.Columns.Add("flightno"); dt.Columns.Add("flightTime",typeof(DateTime)); dt.Columns.Add("arrivalTime",typeof(DateTime)); dt.Columns.Add("passenger"); dt.Rows.Add("AU123",Convert.ToDateTime("2010-12-20 12:30"),Convert.ToDateTime("2010-12-20 15:30"),"小Q"); dt.Rows.Add("AU123",Convert.ToDateTime("2010-12-20 12:30"),Convert.ToDateTime("2010-12-20 15:30"),"小T"); dt.Rows.Add("AU123",Convert.ToDateTime("2010-10-21 12:00"),Convert.ToDateTime("2010-12-21 14:30"),"小N"); dt.Rows.Add("AU124",Convert.ToDateTime("2010-10-22 12:00"),Convert.ToDateTime("2010-12-22 11:30"),"小F"); var query=from t in dt.AsEnumerable() group t by new{t1=t.Field<DateTime>("flightTime"),t2=t.Field<DateTime>("arrivalTime")} into m select new { flightTime=m.Key.t1, arrivalTime=m.Key.t2 }; DataTable dtResult=new DataTable(); dtResult.Columns.Add("flightTime",typeof(DateTime)); dtResult.Columns.Add("arrivalTime",typeof(DateTime)); query.ToList().ForEach(q=>dtResult.Rows.Add(q.flightTime,q.arrivalTime)); }