Lession12 Linq表达式查询

Linq标准查询操作符

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

namespace ConsoleApp1_Linq标准查询
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person("曹操", 50);
            Person P2 = new Person("诸葛亮", 20);
            Person P3 = new Person("刘备", 40);
            Person p4 = new Person("蔡文姬", 16);
            List<Person> list = new List<Person> { p1, P2, P3, p4 };
            List<string> listName = list.Where(p => p.Age > 25) //过滤掉年龄不超过25岁的
                .OrderByDescending(p => p.Age)//按照年龄降序排列
                .Select(p => p.Name) //将查询结果投射成名字
                .ToList(); //将结果转成集合
            Console.WriteLine($"年龄超过25岁的共有{listName.Count}人,按年龄由大到小排的结果是:");
            foreach (var item in listName)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("-------------------------------");
            Console.WriteLine($"所有人的平均年龄是{list.Average(p => p.Age)}");
            Console.WriteLine($"所有人的总年龄是{list.Sum(p => p.Age)}");
            Console.WriteLine($"所有人的最大年龄是{list.Max(p => p.Age)}");
            Console.WriteLine($"所有人的最小年龄是{list.Min(p => p.Age)}");
            Console.WriteLine("-------------------------------------------------------");
            //获取唯一一个年龄大于25的人,如果有多个则抛出异常
            Person p25 = list.Single(p => p.Age > 45);
            Console.WriteLine($"唯一一个年龄大于45岁的是{p25.Age}岁的{p25.Name}");

            Console.ReadKey();
        }
    }

    public class Person
    {
        public Person(string name, int age)
        {
            this.Age = age;
            this.Name = name;

        }
        public int Age { get; set; }
        public string Name { get; set; }
    }
}

在这里插入图片描述

linq表达式查询

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

namespace ConsoleApp1_linq表达式查询
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个数据源
            int[] numbers = new int[] { 0, 1, 2, 3, 4, 5 };
            //创建查询
            IEnumerable<int> numQuery =
             from num in numbers
             where (num % 2) == 0
             select num;
            //执行查询

            foreach (var item in numQuery)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("----------------------------");

            List<int> list = new List<int> { 100, 5, 50, 1, 8, 21, 12 };
            var _list = from i in list
                        where i < 10
                        orderby i
                        select i;

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

        }
    }
}

在这里插入图片描述

Linq扩展方法查询

/*
* public static IEnumerable Where
* (this IEnumerable source, Func<TSource, bool> predicate);
*
* Func<TSource, bool>:委托、
*
* 扩展方法:
* 静态类里写一个静态的方法,参数列表最前面加个this+要扩展到的类型
* 使用场景:在不修改源码的基础上,为其他类型修改方法
*/

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

namespace ConsoleApp1_Linq扩展方法查询
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int> { 10, 9, 5, 20, 24, 6 };
            IEnumerable<int> _list = list.Where(i => i < 10).OrderBy(i => i);
            foreach (var item in _list)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}

在这里插入图片描述

扩展方法

/*
* 扩展方法:
* 静态类里写一个静态的方法,参数列表最前面加个this+要扩展到的类型
* 使用场景:在不修改源码的基础上,为其他类型修改方法
*/

using System;

namespace 扩展方法
{
    class Program
    {
        static void Main(string[] args)
        {
            Cal cal = new Cal();
            int sum = cal.AddNew(1, 2, 3);
            Console.WriteLine(sum);
            Console.ReadKey();
        }
    }
    public class Cal
    {
        public int Add(int a, int b)
        {
            return a + b;
        }

        //public int Add(int a,int b,int c)
        //{
        //    return a + b + c;
        //}
    }
    public static class CalExtend
    {
        public static int AddNew(this Cal cal, int a, int b, int c)
        {
            return a + b + c;
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞鹰@四海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值