.NET , LINQ 扩展方法基础介绍

1 篇文章 0 订阅

using System.Linq;

IEnumerable

 static void F1()
        {
            int[] dt = new int[] { 1,2,3,4,5,6,7,8,9,10};
            IEnumerable<int> items = dt.Where(i => i > 5);
            foreach (var item in items)
            {
                Console.WriteLine(item);
            }

        }

可以将数组 转化为 foreach 数组使用 。
image.png

Where()

image.png

使用方法

image.png

  List<Employee> list = new List<Employee>();
            list.Add(new Employee { Id = 1, Name = "jerry", Age = 28, Gender = true, Salary = 5000 });
            list.Add(new Employee { Id = 2, Name = "jim", Age = 33, Gender = true, Salary = 3000 });
            list.Add(new Employee { Id = 3, Name = "lily", Age = 35, Gender = false, Salary = 9000 });
            list.Add(new Employee { Id = 4, Name = "lucy", Age = 16, Gender = false, Salary = 2000 });
            list.Add(new Employee { Id = 5, Name = "kimi", Age = 25, Gender = true, Salary = 1000 });
            list.Add(new Employee { Id = 6, Name = "nancy", Age = 35, Gender = false, Salary = 8000 });
            list.Add(new Employee { Id = 7, Name = "zack", Age = 35, Gender = true, Salary = 8500 });
            list.Add(new Employee { Id = 8, Name = "jack", Age = 33, Gender = true, Salary = 8000 });

            IEnumerable<Employee> Items1 = list.Where(e => e.Age > 30);
            foreach (var item in Items1)
            {
                Console.WriteLine(item.Name);
            }

Count()

image.png

 static void Main(string[] args)
        {
            List<Employee> list = new List<Employee>();
            list.Add(new Employee { Id = 1, Name = "jerry", Age = 28, Gender = true, Salary = 5000 });
            list.Add(new Employee { Id = 2, Name = "jim", Age = 33, Gender = true, Salary = 3000 });
            list.Add(new Employee { Id = 3, Name = "lily", Age = 35, Gender = false, Salary = 9000 });
            list.Add(new Employee { Id = 4, Name = "lucy", Age = 16, Gender = false, Salary = 2000 });
            list.Add(new Employee { Id = 5, Name = "kimi", Age = 25, Gender = true, Salary = 1000 });
            list.Add(new Employee { Id = 6, Name = "nancy", Age = 35, Gender = false, Salary = 8000 });
            list.Add(new Employee { Id = 7, Name = "zack", Age = 35, Gender = true, Salary = 8500 });
            list.Add(new Employee { Id = 8, Name = "jack", Age = 33, Gender = true, Salary = 8000 });

            IEnumerable<Employee> Items1 = list.Where(e => e.Age > 30);
            foreach (var item in Items1)
            {
                Console.WriteLine(item.Name);
            }


            Console.WriteLine(list.Count());
            Console.WriteLine(list.Count(a=>a.Age>30));

        }

image.png

且: &&

any 是碰到一条就返回,性能比 Count 性能更好
image.png

Single , SingleOrDefault

image.png

//single    //有多条数据就会报错,没有数据的时候也会报错
            Employee e1 =  list.Where(e=>e.Name=="jerry").Single();
            Console.WriteLine(e1.Name);

            Employee e3 = list.SingleOrDefault(i=>i.Name=="123");
            Console.WriteLine(e3 == null);


            Employee e2 = list.First();//有多条数据 返回第一条
            Console.WriteLine(e2);

            Employee e4 = list.FirstOrDefault(i => i.Name == "123");//有多条数据 返回第一条
            Console.WriteLine(e4==null);

using System;
using System.Collections.Generic;
using System.Linq;

namespace lambda表达式应用
{
    class Program
    {
    
    static void Main(string[] args)
        {
            List<Employee> list = new List<Employee>();
            list.Add(new Employee { Id = 1, Name = "jerry", Age = 28, Gender = true, Salary = 5000 });
            list.Add(new Employee { Id = 2, Name = "jim", Age = 33, Gender = true, Salary = 3000 });
            list.Add(new Employee { Id = 3, Name = "lily", Age = 35, Gender = false, Salary = 9000 });
            list.Add(new Employee { Id = 4, Name = "lucy", Age = 16, Gender = false, Salary = 2000 });
            list.Add(new Employee { Id = 5, Name = "kimi", Age = 25, Gender = true, Salary = 1000 });
            list.Add(new Employee { Id = 6, Name = "nancy", Age = 35, Gender = false, Salary = 8000 });
            list.Add(new Employee { Id = 7, Name = "zack", Age = 35, Gender = true, Salary = 8500 });
            list.Add(new Employee { Id = 8, Name = "jack", Age = 33, Gender = true, Salary = 8000 });

            IEnumerable<Employee> Items1 = list.Where(e => e.Age > 30);
            foreach (var item in Items1)
            {
               // Console.WriteLine(item.Name);
            }


            // Console.WriteLine(list.Count());
            //  Console.WriteLine(list.Count(a=>a.Age>30));


            //single    //有多条数据就会报错,没有数据的时候也会报错
            Employee e1 =  list.Where(e=>e.Name=="jerry").Single();
            Console.WriteLine(e1.Name);

            Employee e3 = list.SingleOrDefault(i=>i.Name=="123");
            Console.WriteLine(e3 == null);


            Employee e2 = list.First();//有多条数据 返回第一条
            Console.WriteLine(e2);

            Employee e4 = list.FirstOrDefault(i => i.Name == "123");//有多条数据 返回第一条
            Console.WriteLine(e4==null);


        }

        class Employee
        {
            public long Id { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public bool Gender { get; set; }
            public int Salary { get; set; }
        }



    }
}

Order(), OrderByDescending() 排序

image.png

      //升序
            IEnumerable<Employee> item2 = list.OrderBy(e => e.Age);
            foreach (Employee item in item2)
            {
                Console.WriteLine(item.Age);
            }

            //降序
            IEnumerable<Employee> item3 = list.OrderByDescending(e => e.Age);
            foreach (Employee item in item3)
            {
                Console.WriteLine(item.Age);
            }

限制结果集

image.png

//调过三条数据 取两条数据
            var items = list.Skip(3).Take(2);
            foreach (var e in items)
            {
                Console.WriteLine(e);
            }

image.png
稍微复杂点:

var items3 = list.Where(e => e.Age > 30)
                .OrderBy(e => e.Age).Skip(1).Take(2);
            foreach (var e in items3)
            {
                Console.WriteLine(e.Name);
            }

聚合函数 : MAX MIN Average sum count

image.png

GroupBy() 分组

image.png

    var dt2=   list.GroupBy(e => e.Age);
            foreach (var item in dt2)
            {
                Console.WriteLine(item.Key);
                foreach (var items in item)
                {
                    Console.WriteLine(items.Name);
                }
            }

image.png

投影操作 select()

Select()
image.png
映射:查出来某一个字段或者是字符串

  //投影  select()
           //只取出一个字段的数值
         IEnumerable<int> items4=  list.Select(e => e.Age);
            foreach (var item in items4)
            {
                Console.WriteLine(item);
            }

            IEnumerable<string> items4 = list.Where(e=>e.Age>20).Select(e =>"姓名:"+ e.Name+",年龄:"+e.Age);

            foreach (var item in items4)
            {
                Console.WriteLine(item);
            }

image.png

image.png

匿名类型投影:

   //匿名类型

   var obj1 = new {name="dd",AAA="dts",bbb="12345" };

遍历方便 ** 重点写法**

  var dt1s=  list.Select(e =>new
            { xiaomi = e.Name, dg = e.Age,dts=e.Salary });
            foreach (var item in dt1s)
            {
                Console.WriteLine(item);
            }

image.png

集合类型转换

ToArray()
ToList()
image.png

 IEnumerable<Employee> dtlsit= list.Where(e => e.Salary > 6000);
           List<Employee> dtlist2= dtlsit.ToList();

链式编程

 var dtss1= list.Where(e => e.Id > 2).GroupBy(e => e.Age)
.OrderBy(g => g.Key).Take(3)
.Select(g => new { Nl = g.Key, RS = g.Count(), PJ = g.Average(e => e.Salary) });

另外的语法形式—查询语法

image.png

查询语法 —> 同上效果

image.png

Linq 解决面试中的问题

image.png

案例

image.png

  string s = "1,232,213,45,12,512,543,234";
            string[] strs= s.Split(',');
            IEnumerable<int> intstr= strs.Select(e => Convert.ToInt32(e));
            double avgnumber = intstr.Average();
            Console.WriteLine(avgnumber);
    double avgdt = s.Split(',')
        .Select(e=>Convert.ToInt32(e))
        .Average();
    Console.WriteLine(avgdt);

image.png

        string ss = "dasgdagfdbncewvrthjytbhdgeqcvreq";
           var dss= ss.Where(c => char.IsLetter(c))
                .Select(c => char.ToLower(c))
                .GroupBy(c => c)
                .Select(g => new { g.Key, count = g.Count() })
                .OrderByDescending(g=>g.count)
                .Where(g=>g.count>2);


            foreach (var item in dss)
            {
                Console.WriteLine(item);
            }

image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值