C# Linq 文档(一)

目录

一、概述

二、Where

三、Select

四、GroupBy

五、First / FirstOrDefault

六、Last / LastOrDefault


C# Linq 文档(一)
1.Where,2.Select,3.GroupBy,4.First / FirstOrDefault,5.Last / LastOrDefault

C# Linq 文档(二)
1.OrderBy, 2.OrderByDescending,3.Skip,4.Take,5.Any,6.All

C# Linq 文档(三)
1.Sum / Min / Max / Average,2.Distinct,3.Concat,4.Join,5.ToList ,6.ToArray,7.ToDictionary

C# Linq 文档(四)
1.SelectMany,2.Aggregate,3.DistinctBy,4.Reverse,5.SequenceEqual,6.Zip,7.SkipWhile ,8.TakeWhile


C# Linq 文档(二)_熊思宇的博客-CSDN博客

C# Linq 文档(三)_熊思宇的博客-CSDN博客

C# Linq 文档(四)_熊思宇的博客-CSDN博客


一、概述

语言集成查询 (LINQ) 是一系列直接将查询功能集成到 C# 语言的技术统称。 数据查询历来都表示为简单的字符串,没有编译时类型检查或 IntelliSense 支持。 此外,需要针对每种类型的数据源了解不同的查询语言:SQL 数据库、XML 文档、各种 Web 服务等。 借助 LINQ,查询成为了最高级的语言构造,就像类、方法和事件一样。

对于编写查询的开发者来说,LINQ 最明显的“语言集成”部分就是查询表达式。 查询表达式采用声明性查询语法编写而成。 使用查询语法,可以用最少的代码对数据源执行筛选、排序和分组操作。 可使用相同的基本查询表达式模式来查询和转换 SQL 数据库、ADO .NET 数据集、XML 文档和流以及 .NET 集合中的数据。

二、Where

Where 是 LINQ 的一个操作符,用于筛选满足指定条件的元素。

说白了就是查找元素,这个在 C# 的开发中也是比较常用的,下面看一个例子

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

namespace LinqTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> numList = new List<int> { 1, 4, 5, 24, 43, 67, 12, 90, 15 };
            IEnumerable<int> collect = numList.Where(x => x > 20);
            foreach (int num in collect)
            {
                Console.WriteLine(num);
            }

            Console.ReadKey();
        }
    }
}

运行:

在 where 中,x 所代表的就是 numList 中的每一个元素,Where(x => x > 20) 所指的就是遍历 numList 每一个元素,如果它大于20,那么就添加到 IEnumerable<int> 中,一般情况下,为了让代码更优雅些,IEnumerable<int> 可以不写,直接用 var 代替,当然,你也可以写的更清楚一些,这都是没关系的。

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

namespace LinqTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> numList = new List<int> { 1, 4, 5, 24, 43, 67, 12, 90, 15 };
            var collect = numList.Where(x => x > 20);
            foreach (int num in collect)
            {
                Console.WriteLine(num);
            }

            Console.ReadKey();
        }
    }
}

另外,在 Where 后面可以继续写其他的条件也是没问题的,因为当前的类型本来就是 IEnumerable 类型,它和 List 等数据结构是一样的,都可以使用 Linq 继续操作,下面就是在  Where 后面又加了一个 Where,这么写也是不会报错的,但是不建议这么写,最好是在后面加其他的查询条件,比如 Select

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

namespace LinqTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> numList = new List<int> { 1, 4, 5, 24, 43, 67, 12, 90, 15 };
            var collect = numList.Where(x => x > 20).Where(x => x > 30);
            foreach (int num in collect)
            {
                Console.WriteLine(num);
            }

            Console.ReadKey();
        }
    }
}

运行:

在 Where 的判断条件中,你就当它和 if 中的判断条件一样写,多写几个判断条件也是可以的

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

namespace LinqTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> numList = new List<int> { 1, 4, 5, 24, 43, 67, 12, 90, 15 };
            var collect = numList.Where(x => x > 10 && x < 90);
            foreach (int num in collect)
            {
                Console.WriteLine(num);
            }

            Console.ReadKey();
        }
    }
}

运行:

有人可能会问:“不对啊,我在网上查资料中,和你写的不一样,他们写的有 from, in 等关键字,你这为啥没有啊,你这 Linq 是不是搞错了”?没搞错哈,这都是正常的,比如下面的代码,虽然写法不一样,但效果是一样的,我更推荐使用上面的写法,更容易理解,阅读起来也更加方便。

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

namespace LinqTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> numList = new List<int> { 1, 4, 5, 24, 43, 67, 12, 90, 15 };

            //var collect = numList.Where(x => x > 10 && x < 90);

            var collect = from x in numList where x > 10 && x < 90 select x;
            foreach (int num in collect)
            {
                Console.WriteLine(num);
            }

            Console.ReadKey();
        }
    }
}

运行:

三、Select

Select 是 LINQ 的一个操作符,用于对序列中的每个元素进行转换或投影操作,并返回一个新的序列。

使用 Select 可以对数组中的每个元素进行转换,或者计算,下面用一个简单的例子,将数组中每个元素 + 1

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

namespace LinqTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> numList = new List<int> { 1, 4, 5, 24, 43, 67, 12, 90, 15 };
            var collect = numList.Select(x => x + 1);
            foreach (var x in collect)
            {
                Console.WriteLine(x);
            }

            Console.ReadKey();
        }
    }
}

运行:

同样的,在 Select 执行结束后,依然可以添加其他的 Linq 操作,这个在 Linq 查询中基本都是一样的,后面就不再赘述了。

四、GroupBy

Linq GroupBy 方法用于按照指定的键对集合进行分组,它返回一个根据指定键进行分组的结果集。

先看一段代码,演示 GroupBy 方法 是如何进行分组的

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

namespace LinqTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<People> peopleList = new List<People>()
            {
                new People(){Name="张三", Age=12, Career="学生" },
                new People(){Name="柱子", Age=25, Career="农民" },
                new People(){Name="铁蛋", Age=23, Career="农民" },
                new People(){Name="狗剩", Age=34, Career="职员" },
                new People(){Name="二狗", Age=28, Career="职员" },
            };

            var collect = peopleList.GroupBy(x=> x.Career == "职员");
            foreach (var group in collect)
            {
                Console.WriteLine("Grade {0}:", group.Key);
                foreach (var people in group)
                {
                    Console.WriteLine(people.Name);
                }
            }

            Console.ReadKey();
        }
    }

    class People
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Career { get; set; }
    }
}

结果: 

运行后可以看到,返回的是两组数据,grade 等于 false 是不满足条件的数据,也一并返回了

grade 等于 true 的正是我们想要的数据

五、First / FirstOrDefault

First 方法用于返回集合中的第一个元素。如果集合为空,则会引发一个异常。如果你想要安全地获取第一个元素,可以使用 FirstOrDefault 方法,它会返回默认值(null或0,具体取决于元素类型)而不是引发异常。

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

namespace LinqTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> numList = new List<int> { 1, 4, 5, 24, 43, 67, 12, 90, 15 };
            int firstNumber = numList.First();
            Console.WriteLine("第一个元素:{0}", firstNumber);

            int defaultNumber = numList.FirstOrDefault();
            Console.WriteLine("第一个元素:{0}", defaultNumber);

            int[] emptyNumbers = { };
            int firstOrDefaultNumber = emptyNumbers.FirstOrDefault();
            Console.WriteLine("第一个元素:{0}", firstOrDefaultNumber);

            Console.ReadKey();
        }
    }
}

运行:

六、Last / LastOrDefault

Last 方法用于返回集合中的最后一个元素。如果集合为空,则会引发一个异常。如果你想要安全地获取最后一个元素,可以使用 LastOrDefault 方法,它会返回默认值(null或0,具体取决于元素类型)而不是引发异常。

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

namespace LinqTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> numList = new List<int> { 1, 4, 5, 24, 43, 67, 12, 90, 15 };
            int firstNumber = numList.Last();
            Console.WriteLine("最后一个元素:{0}", firstNumber);

            int defaultNumber = numList.LastOrDefault();
            Console.WriteLine("最后一个元素:{0}", defaultNumber);

            int[] emptyNumbers = { };
            int firstOrDefaultNumber = emptyNumbers.LastOrDefault();
            Console.WriteLine("最后一个元素:{0}", firstOrDefaultNumber);

            Console.ReadKey();
        }
    }
}

运行:

end

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
难得的学习LINQ的中文资料,很全很详细,包内包括两个文档: 一个是语言集成查询.pdf,内容简介如下: 1.LINQ 简介 简要介绍可编写的各种应用程序,以及使用 LINQ 查询可以解决的各种问题。 2.C# 中的 LINQ 入门 描述为理解 C# 文档和示例所应了解的基本情况。 3.Visual Basic 中的 LINQ 入门 描述为理解 Visual Basic 文档和示例所应了解的基本情况。 4.如何:创建 LINQ 项目 介绍生成 LINQ 项目所需的 .NET Framework 版本、引用和命名空间。 5.对 LINQ 的 Visual Studio IDE 和工具支持 描述对象关系设计器、对查询的调试器支持以及其他与 LINQ 相关的 IDE 功能。 6.LINQ 常规编程指南 提供了指向相关主题的链接,这些主题包含有关如何使用 LINQ 进行编程的信息,例如标准查询运算符、表 达式目录树和查询提供程序。 7.LINQ to Objects 包含指向相关主题的链接,这些主题说明如何使用 LINQ to Objects 来访问内存中的数据结构。 8.LINQ to XML 包含指向说明如何使用 LINQ to XML 的主题的链接,此功能可提供文档对象模型 (DOM) 的内存中文档修改 功能,并且支持 LINQ 查询表达式。 9.LINQ to ADO.NET(门户页) 提供 linq_dataset 和 vbtecdlinq 相关文档的入口点。 LINQ to DataSet 使您可以通过使用为其他数据源提供的相同查询功能,在 DataSet 中加入更丰富的查询功能。 LINQ to SQL 为将关系数据作为对象进行管理提供了运行时基础结构。 10.补充的 LINQ 资源 指向 LINQ 相关信息的其他联 另一个是LINQ to ADO.net.pdf 描述linq如何与ado.net结合使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

熊思宇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值