码迷,mamicode.com
首页 > 其他好文 > 详细

Linq 高级应用实例

时间:2015-01-19 20:42:00      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(@"C:\Users\UserAccount\Desktop\20131103203219.txt");
            string record = string.Empty;

            StringBuilder sb = new StringBuilder();
            sb.Append([);
            while ((record = sr.ReadLine()) != null)
            {
                sb.Append(record + ,);
            }
            sb.Remove(sb.ToString().Length - 1, 1);
            sb.Append(]);

            JavaScriptSerializer js = new JavaScriptSerializer();
            List<GPS> list = js.Deserialize(sb.ToString(), typeof(List<GPS>)) as List<GPS>;

            var list2 = from x in list
                        where x.Distance % 400 < 10 && x.SpeedMeter != 0
                        orderby x.GPSTime ascending
                        select new { x.DuringTime, x.Distance, x.SpeedMeter };

            // 求在起点处几个点的平均速度
            var list3 = from x in list2
                        orderby (int)x.Distance / 400 ascending
                        group x by (int)x.Distance / 400 into gx
                        select new
                        {
                            gx.Key,
                            Distance = gx.Min(p => p.Distance),
                            Speed = gx.Average(p => p.SpeedMeter)
                        };

            // 求每一圈的平均速度
            var list4 = from x in list
                        where x.SpeedMeter != 0
                        group x by (int)(x.Distance / 400) into gx
                        orderby gx.Key ascending
                        select new
                        {
                            gx.Key,
                            Distance = gx.Min(p => p.Distance),
                            Speed = gx.Average(p => p.SpeedMeter)
                        };

            StreamWriter sw = new StreamWriter(@"T:\kongston.txt");
            foreach (var g in list4)
            {
                sw.WriteLine(js.Serialize(g));
                Console.WriteLine(js.Serialize(g));
            }

            sr.Close();
            sw.Close();
            sr.Dispose();
            sw.Dispose();
        }
    }
}



GPS类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class GPS
    {
        public double Longitude { get; set; }

        public double Latitude { get; set; }

        public double Accuracy { get; set; }

        public double Altitude { get; set; }

        public double SpeedMeter { get; set; }

        private long _GPSTime;

        public long GPSTime
        {
            get
            {
                return this._GPSTime;
            }
            set
            {
                this._GPSTime = value;

                // C# 解析GPS返回的时间
                DateTime dt = new DateTime(1970, 1, 1).AddMilliseconds(value).AddHours(8);
                NormalTime = dt;
                StringTime = dt.ToString("yyyy-MM-dd HH:mm:ss");
            }
        }

        public DateTime NormalTime { get; set; }

        public string StringTime { get; set; }

        public double Bearing { get; set; }

        public int DuringTime { get; set; }

        public double Distance { get; set; }
    }
} 

 

Linq 高级应用实例

标签:

原文地址:http://www.cnblogs.com/mtsl/p/4234483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!