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

动态Linq(结合反射)

时间:2014-05-16 00:40:13      阅读:414      评论:0      收藏:0      [点我收藏+]

标签:des   blog   class   code   c   java   

 

  这篇文章决定对最近一个单机版Web程序用到的东西总结一下。

一、反射Linq之OrderBy

  动态Linq结合反射对某字段排序:

bubuko.com,布布扣
bubuko.com,布布扣
namespace 动态Linq
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> ListP = new List<Person>();
            ListP.Add(new Person(1, "刘备", 40));
            ListP.Add(new Person(2, "关羽", 35));
            ListP.Add(new Person(3, "张飞", 29));

            Hashtable ht = new Hashtable();
            ht.Add("SortName","Id");
            ht.Add("SortOrder","desc");

            List<Person> ListT = PageSortList<Person>(ListP, ht);
            foreach (Person p in ListT)
            {
                Console.WriteLine(p.Id);
            }

            Console.ReadKey();
        }

        //分页排序
        public static List<T> PageSortList<T>(List<T> ListT, Hashtable ht)
        {
            string SortName = ht["SortName"].ToString();
            string SortOrder = ht["SortOrder"].ToString();
            if (!string.IsNullOrEmpty(SortName))
            {
                if (SortOrder.ToLower() == "desc")
                {
                    ListT = ListT.OrderByDescending(m => m.GetType().GetProperty(SortName).GetValue(m, null)).ToList();
                }
                else
                {
                    ListT = ListT.OrderBy(m => m.GetType().GetProperty(SortName).GetValue(m, null)).ToList();
                }
            }
            return ListT;
        }
    }

    public class Person
    {
        public Person(int id, string name, int age) { Id = id; Name = name; Age = age; }

        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
bubuko.com,布布扣
bubuko.com,布布扣

  输出如下:

  bubuko.com,布布扣

  唯一要注意的东西,刚开始写的不正确,实际上排序始终都是对属性的值排序。这种东西有没有用呢?

  线上系统一般很少用,但是最近项目要求做一个离线版Web,离线操作,连线导入数据。Oracle转Xml,如果不大量采用泛型与反射,估计得写一年左右。

二、反射Linq之Where

  动态Linq使用Where

bubuko.com,布布扣
bubuko.com,布布扣
namespace 动态Linq
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> ListP = new List<Person>();
            ListP.Add(new Person(1, "刘备", 40));
            ListP.Add(new Person(2, "关羽", 35));
            ListP.Add(new Person(3, "张飞", 29));

            Hashtable ht = new Hashtable();
            ht.Add("Name","关羽");

            List<Person> ListT = PageSortList<Person>(ListP, ht);
            foreach (Person p in ListT)
            {
                Console.WriteLine(p.Id);
            }

            Console.ReadKey();
        }

        //分页排序
        public static List<T> PageSortList<T>(List<T> ListT, Hashtable ht)
        {
            string Key = ht.Cast<DictionaryEntry>().FirstOrDefault().Key.ToString();
            string Value = ht.Cast<DictionaryEntry>().FirstOrDefault().Value.ToString();
            ListT = ListT.Where(m => m.GetType().GetProperty(Key).GetValue(m, null).ToString() == Value).ToList();
            return ListT;
        }
    }

    public class Person
    {
        public Person(int id, string name, int age) { Id = id; Name = name; Age = age; }

        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}
bubuko.com,布布扣
bubuko.com,布布扣

  输出如下:

  bubuko.com,布布扣

动态Linq(结合反射),布布扣,bubuko.com

动态Linq(结合反射)

标签:des   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/mingxuantongxue/p/3730082.html

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