C# 映射,反射简单使用

首先赋予要映射的类get,set属性

public class GetLifeCheck
    {
        public string orgname { get; set; }
        public string vDevicedesc{ get; set; }
        public DateTime LifeCycle { get; set; }
    }

然后在需要用到的地方使用

        List<GetLifeCheck> re = Db.Queryable("devices", "dev")
            .AddJoinInfo("gateway", "gw", "dev.Gatewayaddress=gw.Gatewayaddress", JoinType.Inner)
            .AddJoinInfo("devicetypedetail", "dt", "dev.detailid=dt.detailid", JoinType.Inner)
            .AddJoinInfo("devicetype", "dtp", "dt.tiDeviceType=dtp.tiDeviceType", JoinType.Inner)
            .AddJoinInfo("onlineorg", "org", "gw.orgid=org.orgid", JoinType.Inner)
            .AddJoinInfo("DLOrg", "dl", "org.ID1=dl.onlineorg_ID", JoinType.Inner)
            .AddJoinInfo("Dept", "dept", str1)
            .Where(str2 + "'" + id + "'" + "and gw.gatewaytype='1'and dev.LifeCycle is not null and dev.LifeCycle<GETDATE()")
            .Select<GetLifeCheck>("*").ToList();

反射主要用在类的处理,简单的反射引用:

var re = Db.Queryable<V_DLMiniStation>().Where(it => it.g1 == id).Select(it =>
                 new { it.ID, it.Lat, it.Long, it.Master, it.sLevel, it.sName, it.Address, it.Tel }).ToList();

            var res = re.Select(a =>
            {
                Dictionary<string, string> tmp = new Dictionary<string, string>();
                foreach (System.Reflection.PropertyInfo p in a.GetType().GetProperties())
                {
                    if (p.Name != "Lat" && p.Name != "Long" && p.Name != "ID")
                    {
                        tmp.Add(p.Name, p.GetValue(a).ToString());
                    }
                }
                return new
                {
                    lng = a.Long,
                    lat = a.Lat,
                    id = a.ID,
                    info = tmp.Select(di => new { name = di.Key, value = di.Value })
                };
            });
            return res;

对list结果集进行简单的select操作(lambda表达式真的方便)

 public class Info
        {
            public string name;//姓名
            public DateTime registime;//入学时间
        }
        public static void Main()
        {
            List<Info> infos = new List<Info>();
            Random random = new Random();
            for (int i = 0; i < 5; i++)
            {
                Info info = new Info
                {
                    name = "tom" + i,
                    registime = DateTime.Now.AddDays(-random.Next(10, 100))
                };
                infos.Add(info);
            }
            DateTime dt = DateTime.Now;
            var re=infos.Select(s => new { name = s.name, registime = s.registime, total = (dt - s.registime).Days });
            
            foreach(var s in re)
            {
                Console.WriteLine("姓名 "+s.name + "入学时间 " + s.registime + "入学天数 " + s.total);
            }
            Console.ReadKey();
        }

select可以大括号里面写方法晒选,在return

            var re=infos.Select(s => 
            {
                int total = (DateTime.Now - s.registime).Days;
                return new
                {
                    name = s.name,
                    registime = s.registime,
                    total = total
                };
            }

与select相似 对于list还可以用ForEach ,ForEach 里面传action,对于list里面每项执行action


版权声明:本文为qq_41918045原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。