zoukankan      html  css  js  c++  java
  • c#中的linq一

    c#中的linq

     测试数据:

         

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LinqInfo
    {
        public class Stu
        {
            public int ID { set; get; }
    
            public string Name { get; set; }
    
            public string Tel { get; set; }
    
            public string Address { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                List<Stu> list = new List<Stu>()
                {
                    new Stu{ID=1,Name="jack1",Tel="110",Address="ChengDu"},
                    new Stu{ID=2,Name="jack2",Tel="112",Address="ChengDu2"},
                    new Stu{ID=3,Name="jack3",Tel="113",Address="ChengDu3"},
                    new Stu{ID=4,Name="jack4",Tel="114",Address="ChengDu4"},
                    new Stu{ID=5,Name="jack5",Tel="115",Address="ChengDu5"}
    
                };
                var obj = new { ID=12,Name="Tom" };  //匿名对象;
                //然后我们来进行简单的linq的复习和使用滴呀
                //借助于LINQ技术,我们可以使用一种类似SQL的语法来查询任何形式的数
                //同一套LINQ语法,它能支持LINQ TO OBJECCT、LINQ TO XML、LINQ TO DATABASE。复用、减少开发工作量及降低学习成本等好处都是不言而喻的
    
                Console.WriteLine();
            }
        }
    }

    当我们需要在集合中查询一些对象,数据的时候

                List<Stu> listNew = new List<Stu>();
                //以前当我们要查询一个值的时候;
                foreach(Stu s in list)
                {
                    if(s.Name=="jack1")
                    {
                        listNew.Add(s);
                    }
                }

    或者你会这么写:

           //或者你会这么写;
            static bool IsExist(Stu s)
            {
                return s.Name =="jack1";
            }
    
            foreach (Stu s in list)
            {
               if (IsExist(s))
                {
                    listNew.Add(s);
                }
            }

    或者你会这么优化:

         public delegate bool ExistStu(Stu s);
    
            static List<Stu> GetList(IList<Stu> list, ExistStu fun)
            {
                List<Stu> listNew = new List<Stu>();
                foreach (Stu s in list)
                {
                    if (fun(s))
                    {
                        listNew.Add(s);
                    }
                }
                return listNew;
            }

    //调用:
    List<Stu> lis = GetList(list,IsExist);

    再或者:

                //或者你你还可以使用匿名委托;
                List<Stu> listInfo = GetList(list, delegate(Stu s) { return s.Name == "jack1" ? true : false; });
    
                //或者你可以使用lambda表达式
                List<Stu> listStu = GetList(list,p=>p.Name=="jack1");

    然后就是扩展方法的使用:

        //然后是扩展方法;
        public static class Helper
        {
            //申明委托;
            public delegate bool ExistStu(Stu s);
    
            public static IList<Stu> GetList(this IList<Stu> listStu, ExistStu func)
            {
                List<Stu> result = new List<Stu>();
                foreach (Stu s in listStu)
                {
                    if (func(s))
                    {
                        result.Add(s);
                    }
                }
                return result;
            }
    
    
        }
         //然后是扩展方法的调用;
         list.GetList(p => p.Name == "jack1");  //这样更直观更完美滴呀

     当然,我们也可以将上面的方法扩展成泛型的;

    public static class Helper2
        {
            public delegate bool Condition<T>(T t);
    
            public static IEnumerable<T> GetStuList<T>(this IEnumerable<T> items, Condition<T> condition)
            {
                foreach (T t in items)
                {
    
                    if (condition(t))
                    {
                        yield return t;  //yield 关键字的使用
                    }
                }
            }
    
        }

     继续开发实例:

                //当我们需要赛选数据;
                var temp=new List<Stu>();
                if (list.Count > 0)
                {
                    //以前我们会怎么写;
                    foreach (var obj in list)
                    {
                        if (obj.Name == "jack3")
                        {
                            temp.Add(obj);
                        }
                    }
                }
    
                //现在我们可以优化这么写滴啊
                var tempData = new List<Stu>();
                if (list.Count > 0)
                {
                    //先添加命名空间 using System.Linq;
                    tempData.AddRange(list.Where(obj=>obj.Name=="jack3"));
                }
    
                //或则你可能会这么写滴呀;
                var Names = new List<string>();
                if (list.Count > 0)
                {
                    foreach (var obj in list)
                    {
                        Names.Add(obj.Name); //再进一步的进行代码的优化的滴 呀;
                    }
                }
    
                //然后你可以这么优化的滴呀
                var nameList=new List<string>();
                if (list.Any())  //你就可以这样使用any 来进行优化滴呀;
                {
                    nameList.AddRange(list.Select(obj => obj.Name)); //也可以进行这样的基本优化地哎呀
                }

     这个就是我们优化之路滴呀;

  • 相关阅读:
    exp迁移测试库10.2.0.5
    DG_Check检测
    DG Switch over
    CPU查询
    记录数据库中,段大小的数据增长情况
    C++ 多态
    java反射
    git的基本概念
    实现MySQL的Replication
    网页只允许中国用户访问
  • 原文地址:https://www.cnblogs.com/mc67/p/5648225.html
Copyright © 2011-2022 走看看