• linq的使用


     1.匿名方法 lambda表达式

    2.匿名类 var 扩展方法
    3.linq to object:
    4.yield 爹迭代器:完成了数据的按需获取,延迟加载,yieId 跟IEnumerable 配对使用


    5.Linq常用方法介绍:
    Linq--Where:把对数据过滤的通用操作完成,把可变的逻辑交给委托
    Linq--select
    Linq--orderby
    Linq--orderby
    Linq--group s by s.ClassId into sg select sg
    Linq--Find
    Linq--FindAll
    6.lambda和linq的关系?
    lambda是实例化委托的快捷方式, 是方法
    linq是基于委托的封装,逻辑解耦,代码重用, 是帮助类库

    7.Linq to object,Linq to sql,Linq to XML 的区别:
    Linq to object:针对 IEnumerable数据操作,指的是内存数据;where,order by ,select,可变的部分通过委托来实现;
    Linq to sql:针对IQueryrable数据操作,操作数据库程序访问数据库,可变的sql,通过表达式目录树来传递,可以解析的
    Linq to xml: 封装一下通用的对xml 文件的操作,可变的通过委托来传递;
    Linq to Nosql:等待大家实现
    Linq to Redis:
    Linq to Cache:
    Linq to Excel:
    Linq to Json:


    //扩展方法+func
    public static List<Student> ListsWhere(this List<Student> list, Func<Student, bool> func)
    {

    var listnew = new List<Student>();
    foreach (Student item in list)
    {
    if (func.Invoke(item))
    {
    item.ClassId = 2000;
    listnew.Add(item);
    }
    }
    return listnew;
    }
    //yieId+IEnumerable
    public static IEnumerable<T> ListWhere<T>(this IEnumerable<T> list, Func<T, bool> func)
    {

    foreach (var item in list)
    {
    if (func.Invoke(item))
    {
    yield return item;//yieId 跟IEnumerable 配对使用
    }
    }
    }
    //linq
    var lst= studentList.Where<Student>(s=>s.Age <30)
    .Select(s=> new
    {
    IdName=s.Id+s.Name,
    NewName=s.Name+"_VIP"
    })
    .OrderBy(s=>s.Id)
    .OrderByDescending(s=>s.ClassId)
    .Skip(2)//跳过前2条
    .Take(3)// 获取3条
    ;
    //左连接
    var list =from s in studentList
    join c in classList on s.ClassId equals c.Id
    join scList
    from sc in scList.DefaultIfEmpty()
    select new
    {
    Name=s.Name,
    ClassName=sc==null ? "无班级" : sc.ClassName
    };
    var list =studentList.Where<Student>(s=>s.age <30)
    .Where(s=>new int[]{1,2,3,4}.Contains(s.Id));

    linq的使用

    1.List 泛型扩展方法

    2.list 条件赛选;

    3.list 更新item;

    public static List<T> ListWhere<T>(this List<T> list,Func<T,bool> func)
    {
    var listnew = new List<T>();
    foreach (var item in list)
    {
    if (func.Invoke(item))
    {
    listnew.Add(item);
    }
    }
    return list;
    }

    public static List<Student> ListsWhere(this List<Student> list, Func<Student, bool> func)
    {
    var listnew = new List<Student>();
    foreach (Student item in list)
    {
    if (func.Invoke(item))
    {
    item.ClassId = 2000;
    listnew.Add(item);
    }
    }
    return listnew;
    }

    public static List<Student> GetList(List<Student> list)
    {
    //var listnew = new List<Student>();
    foreach (Student item in list)
    {
    if (item.Id==4)
    {
    item.ClassId = 3000;
    //listnew.Add(item);
    }
    }
    return list;
    }

  • 相关阅读:
    最短路径问题大总结(提纲)
    单源最短路——Bellman-Ford算法
    多源最短路——Floyd算法
    Bracket Sequences Concatenation Problem括号序列拼接问题(栈+map+思维)
    数位DP
    C++ string中的find()函数
    Planning The Expedition(暴力枚举+map迭代器)
    8月5号团队赛补题
    8月3号水题走一波-个人赛五
    Walking Between Houses(贪心+思维)
  • 原文地址:https://www.cnblogs.com/csj007523/p/14311082.html
Copyright © 2020-2023  润新知