C军

不玩博客了!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

一、反射Linq之OrderBy

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

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

  输出如下:

  

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

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

二、反射Linq之Where

  动态Linq使用Where

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

  输出如下:

  

posted on 2013-12-03 18:15  逆心  阅读(3756)  评论(3编辑  收藏  举报