数据库表记录如:检测时间                库房名     监测点名    温度值
2010年8月25日14:51:01   1号库房     1号点       27.35
2010年8月25日14:51:01   13号库房    1号点       27.35
2010年8月25日14:51:01   13号库房    2号点       27.35
2010年8月25日14:51:01   13号库房    3号点       27.35
2010年8月25日14:51:01   21号库房    2号点       27.35要求:按时间分组,每组内按库房名和监测点名中的首数字进行排序,难啊⊙﹏⊙b汗,有知道怎么用linq方式实现的本人不胜感激涕零!

解决方案 »

  1.   

    public class Record
            {
                public DateTime CheckTime { get; set; }
                public string StoreHouse { get; set; }
                public string Position { get; set; }
                public float Value { get; set; }
            }
            static void Main(string[] args)
            {
                List<Record> rList = new List<Record>
                {
                    new Record{CheckTime=new DateTime(2010,8,25,14,51,01),StoreHouse="1号库房",Position="1号点",Value=27.35f},
                    new Record{CheckTime=new DateTime(2010,8,25,14,51,02),StoreHouse="13号库房",Position="1号点",Value=27.35f},
                    new Record{CheckTime=new DateTime(2010,8,25,14,51,02),StoreHouse="13号库房",Position="2号点",Value=27.35f},
                                    new Record{CheckTime=new DateTime(2010,8,25,14,51,03),StoreHouse="21",Position="1号点",Value=27.35f},
                    new Record{CheckTime=new DateTime(2010,8,25,14,51,03),StoreHouse="13号库房",Position="1号点",Value=27.35f}            };
                var groups = from g in rList group g by g.CheckTime;            foreach (var g in groups)
                {
                    var orderVal = from r in g orderby r.StoreHouse[0] ascending orderby r.Position[0] ascending select r;                foreach (Record r in orderVal)
                    {
                        Console.WriteLine("time={0},storehouse={1},postion={2}", r.CheckTime, r.StoreHouse, r.Position);
                    }
                }        }
      

  2.   

      List<Dot> dots = new List<Dot>();
                dots.Add(new Dot {  dt=DateTime.Now, Father="1号",Name="1号点",Value=12.121f});
                dots.Add(new Dot { dt = new DateTime(2010,2,3,21,21,12), Father = "2号", Name = "2号点", Value = 12.121f });
                dots.Add(new Dot { dt = new DateTime(2010, 2, 3, 1, 1, 4), Father = "3号", Name = "3号点", Value = 12.121f });
                dots.Add(new Dot { dt = new DateTime(2010, 3, 3, 23, 2, 3), Father = "10号", Name = "10号点", Value = 12.121f });
                dots.Add(new Dot { dt = new DateTime(2010, 4, 1, 21, 21, 6), Father = "11号", Name = "11号点", Value = 12.121f });
                dots.Add(new Dot { dt = new DateTime(2010, 1, 2, 8, 22, 8), Father = "12号", Name = "12号点", Value = 12.121f });
                dots.Add(new Dot { dt = new DateTime(2010, 2, 12, 9, 3, 12), Father = "20号", Name = "20号点", Value = 12.121f});
                dots.Add(new Dot { dt = new DateTime(2010, 7, 13, 11, 6, 11), Father = "21号", Name = "21号点", Value = 12.121f });
                dots.Add(new Dot { dt = DateTime.Now, Father = "21号", Name = "21号点", Value = 12.121f });
                dots.Add(new Dot { dt = DateTime.Now, Father = "2号", Name = "2号点", Value = 12.121f});
                dots.Add(new Dot { dt = DateTime.Now, Father = "10号", Name = "10号点", Value = 12.121f });
                dots.Add(new Dot { dt = DateTime.Now, Father = "11号", Name = "11号点", Value = 12.121f });
                 //时间分组
                var ds = from d in dots group d by d.dt.ToString();
                DataTable table = new DataTable();
                table.Columns.Add("1");
                table.Columns.Add("2");
                table.Columns.Add("3");
                table.Columns.Add("4");
                foreach (var g in ds)
                {
                    //因为每个Father名称都以数字开始命名,故(ˇˍˇ) 想按数字大小排序,而非按字符串"数字"大小排序。应该怎么实现?
                    var ds2 = from d2 in g orderby  d2.Father.Substring(0,2) orderby d2.Name select d2;                foreach (var d in ds2)
                    {
                        table.Rows.Add(new object[] {d.dt,d.Father,d.Name,d.Value });
                    }
                }
                dataGridView1.DataSource = table.DefaultView;
      

  3.   

      List<Dot> dots = new List<Dot>();
                dots.Add(new Dot {  dt=DateTime.Now, Father="1号",Name="1号点",Value=12.121f});
                dots.Add(new Dot { dt = new DateTime(2010,2,3,21,21,12), Father = "2号", Name = "2号点", Value = 12.121f });
                dots.Add(new Dot { dt = new DateTime(2010, 2, 3, 1, 1, 4), Father = "3号", Name = "3号点", Value = 12.121f });
                dots.Add(new Dot { dt = new DateTime(2010, 3, 3, 23, 2, 3), Father = "10号", Name = "10号点", Value = 12.121f });
                dots.Add(new Dot { dt = new DateTime(2010, 4, 1, 21, 21, 6), Father = "11号", Name = "11号点", Value = 12.121f });
                dots.Add(new Dot { dt = new DateTime(2010, 1, 2, 8, 22, 8), Father = "12号", Name = "12号点", Value = 12.121f });
                dots.Add(new Dot { dt = new DateTime(2010, 2, 12, 9, 3, 12), Father = "20号", Name = "20号点", Value = 12.121f});
                dots.Add(new Dot { dt = new DateTime(2010, 7, 13, 11, 6, 11), Father = "21号", Name = "21号点", Value = 12.121f });
                dots.Add(new Dot { dt = DateTime.Now, Father = "21号", Name = "21号点", Value = 12.121f });
                dots.Add(new Dot { dt = DateTime.Now, Father = "2号", Name = "2号点", Value = 12.121f});
                dots.Add(new Dot { dt = DateTime.Now, Father = "10号", Name = "10号点", Value = 12.121f });
                dots.Add(new Dot { dt = DateTime.Now, Father = "11号", Name = "11号点", Value = 12.121f });
                 //时间分组
                var ds = from d in dots group d by d.dt.ToString();
                DataTable table = new DataTable();
                table.Columns.Add("1");
                table.Columns.Add("2");
                table.Columns.Add("3");
                table.Columns.Add("4");
                foreach (var g in ds)
                {
                    //因为每个Father名称都以数字开始命名,故(ˇˍˇ) 想按数字大小排序,而非按字符串"数字"大小排序。应该怎么实现?
                    var ds2 = from d2 in g orderby  d2.Father.Substring(0,2) orderby d2.Name select d2;                foreach (var d in ds2)
                    {
                        table.Rows.Add(new object[] {d.dt,d.Father,d.Name,d.Value });
                    }
                }
                dataGridView1.DataSource = table.DefaultView;