• C# List的使用


    一、List排序

      采用自定义Sort比较器和Linq

    class Persion
        {
            public string name;
            public int age;
    }
    //自定义Sort方法
    private static void sortListClass()
            {
                Persion p1 = new Persion() { name = "huyong", age = 27 };
                Persion p2 = new Persion() { name = "tansha", age = 25 };
                List<Persion> listPersion = new List<Persion>();
                listPersion.Add(p1);
                listPersion.Add(p2);
                listPersion.Sort(sortage);
                foreach (var item in listPersion)
                {
                    Console.WriteLine(item.name + ":" + item.age);
                }
            }
            static int sortage(Persion x, Persion y)
            {
                if (x.age < y.age) return -1;                //按age从大到小 降序排列
                else if (x.age == y.age) return 0;
                else return 1;
            }
    //linq方法
     private static void sortLinq()
            {
                Persion p1 = new Persion() { name = "huyong", age = 27 };
                Persion p2 = new Persion() { name = "tansha", age = 25 };
                Persion p3 = new Persion() { name = "tansha", age = 27 };
                List<Persion> listPersion = new List<Persion>();
                listPersion.Add(p1);
                listPersion.Add(p2);
                listPersion.Add(p3);
    
     var lList = from t in listPersion where (t.age == 27) orderby t.name ascending select t;
                foreach (var item in lList)
                {
                    Console.WriteLine(item.name + ":" + item.age);
                }
            }
    View Code

    二、用Linq将两个list合并为一个list

      //linq合并两个list,根据某一相同字段
    private static void mergeList()
            {
                Persion p1 = new Persion() { name = "hy", province = "jx" };
                Persion p2 = new Persion() { name = "tansha", province = "hn" };
                Persion p3 = new Persion() { name = "ht", province = "gd" };
                List<Persion> listPersion = new List<Persion>();
                listPersion.Add(p1);
                listPersion.Add(p2);
                listPersion.Add(p3);
    
                Persion p11 = new Persion() { name = "hy", age = 23, sex = "" };
                Persion p21 = new Persion() { name = "tansha", age = 12, sex = "" };
                Persion p31 = new Persion() { name = "ht", age = 7, sex = "" };
                List<Persion> listPersion2 = new List<Persion>();
                listPersion2.Add(p11);
                listPersion2.Add(p21);
                listPersion2.Add(p31);
    
                //linq合并两个list,根据某一相同字段
                var list = from pp1 in listPersion
                           join pp2 in listPersion2 on pp1.name equals pp2.name
                           select new Persion
                           {
                               name = pp1.name,
                               province = pp1.province,
                               age = pp2.age,
                               sex = pp2.sex
                           };
                foreach (var item in list)
                {
                    Console.WriteLine(item.name + ":" + item.age + ":" + item.sex + ":" + item.province);
                }
            }
    View Code

    三、List里面的某个数据进行操作并保存

    lists.Where(q =>q.SellChannelId == ((int)SellChannelType.PB).ToString()).ToList().ForEach(q => q.index = 10);

    四、List<T>转化为IQueryable<T>

      不能直接转换,

    IQueryable在数据库端中查询,

    IEnumerable在内存上查询。

    List<T>支持后者。可以用 list.AsQueryable<T>转换。

    五、使用LINQ从List<T>集合中删除指定集合元素

    实体类(Author):

    public class Author{
        public int id{get;set;}
       public string firstname{get;set;}
       public string lastname {get;set;}
    }

    比如有如下的LINQ查询语句:

    var authors = from x in authorsList
                  select x;
    

    其中,需要从查询集合中删除 firstname=’Bob’ 的所有用户,应该如何实现呢?

    方案一:

    authorsList = authorsList.Where(x => x.FirstName != "Bob").ToList();
    //或者
    authorsList.RemoveAll(x => x.firstname == "Bob");

    方案二:

    var authorsList = new List<Author>()
    {
        new Author{ Firstname = "Bob", Lastname = "Smith" },
        new Author{ Firstname = "Fred", Lastname = "Jones" },
        new Author{ Firstname = "Brian", Lastname = "Brains" },
        new Author{ Firstname = "Billy", Lastname = "TheKid" }
    };
    
    var authors = authorsList.Where(a => a.Firstname == "Bob");
    authorsList = authorsList.Except(authors).ToList();
    authorsList = authorsList.Except(authorsList.Where(a=>a.Firstname=="Billy")).ToList();
     






  • 相关阅读:
    无缝滚动效果实现
    js模拟滚动条插件一二
    powerDesigner15.1破解
    解决asp.net 中GridView中隐藏过长的数据(使用ToolTip)
    IE6下弹出div覆盖select下拉框
    sql中的常用函数结合实现截取替换功能
    【.NET重修计划】数组,集合,堆栈的问题
    Redis可视化工具推荐
    Redis中String类型的相关命令操作
    Redis的五大数据类型以及key的相关操作命令
  • 原文地址:https://www.cnblogs.com/peterYong/p/10881979.html
Copyright © 2020-2023  润新知