Linq 高级应用实例

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; }
    }
} 

 

转载于:https://www.cnblogs.com/mtsl/p/4234483.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值