LINQ - 基本语法

LINQ - 基本语法 首页 / LinQ入门教程 / LINQ - 基本语法

Before proceeding to the LINQ查询语法, we will discuss some basic terms related to LINQ Syntax:

编写LINQ查询的要求

要编写LINQ查询,我们需要以下三项内容:

  1. Data Source (in-memory objects, SQL, XML)
  2. Query
  3. Execution of the Query

什么是查询?

查询只不过是一组指令。查询应用于数据源(即内存中对象、SQL、XML等)以执行操作(即CRUD操作)并显示来自该查询的输出的形状。这意味着查询不负责将要输出的内容;相反,它负责输出的形状。

链接:https://www.learnfk.comhttps://www.learnfk.com/linq/linq-syntax.html

来源:LearnFk无涯教程网

每个查询由三个部分组成,它们是:

  1. Initialization(to work with a particular data source)
  2. Condition(where, filter, sorting condition)
  3. Selection (single selection, group selection or joining)

LINQ is an acronym of "Language Integrated Query." The main feature is to allow the users to write the queries in the style of SQL queries within the code using the query syntaxes.

The .NET framework provides the set of built-in query keywords in LINQ to allow the users to write the SQL style queries.

LINQ有三种编写查询的方式:

  • Using Query Syntax
  • Using Method Syntax
  • Using Mixed Syntax

LINQ查询语法

LINQ是以可访问和可读的格式编写复杂LINQ查询的最简单方法之一。此类查询的语法与SQL查询非常相似。

LINQ的语法为:

LINQ Syntax

在LINQ中,我们按照一定的规则编写LINQ查询。语法与SQL不同。要用LINQ编写查询,需要遵循如下语法层次结构:

from <variable> in <collection>
< where, joining, grouping, operators, etc.> <lambda expression>
<select or groupBy operator> <format the results>

We will follow this order when we are writing the queries in LINQ. The starting point of LINQ will start from the "from" keyword, which is followed by a user-defined variable followed by in which specifies the source of the collection or reference of the data, which is followed by a where clause. If there is a particular query that can be used before the select ion to filter the record and select is followed by and group by into the clause.

The order of the clauses in LINQ query will be like, as shown below:

Clauses Description
From [Identifier]
In [Source Collection]
Let [Expression]
Where [Boolean Expression]
order by [Expression]
Select [Expression]
group by [Expression]
into [Expression]

C#中LINQ查询的代码片段

LINQ查询语法 in C#

int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
IEnumerable<int> result = from numbers in Num
                                where numbers >3
                                select numbers;

现在我们将通过以下示例了解如何使用LINQ查询语法:

示例:我们有一个整数列表,需要编写一个返回所有大于5的整数的LINQ查询,这里我们将创建一个控制台应用程序。

使用查询语法的示例

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;

namespace ConsoleApp1
{
classProgram
    {
staticvoid Main(string[] args)
        {
//Data Source
            ListintegerList = new List()
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };
//LINQ Query using Query Syntax
varQuerySyntax = fromobjinintegerList
whereobj> 5
selectobj;
//Execution
foreach (var item inQuerySyntax)
            {
Console.Write(item + " ");
            }
Console.ReadKey();
        }
    }
}

Now we will run the application, and it will display the values 6, 7, 8,9,10, as shown below in the output in the console window.

输出

LINQ Syntax
LINQ Syntax

LINQ方法语法

方法语法成为当今最流行的编写LINQ查询的方法。它使用lambda表达式来定义查询条件。方法语法很容易编写简单的查询来对特定数据源执行读写操作。对于复杂的查询,与查询语法相比,方法语法有点难。

在此方法中,LINQ查询是通过使用多个方法并将它们与点(.)组合在一起来编写的。

对于此方法,语法为:

LINQ Syntax

现在,我们将使用LINQ方法语法重写相同的示例:

using System;
usingSystem.Collections.Generic;
usingSystem. Linq;
usingSystem. Text;
usingSystem.Threading.Tasks;

namespace ConsoleApp1
{
classProgram
    {
staticvoid Main(string[] args)
        {
//Data Source
            ListintegerList = new List()
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };
//LINQ Query using Method Syntax
varMethodSyntax = integerList.Where(obj =>obj> 5).ToList();
//Execution
foreach (var item inMethodSyntax)
            {
Console.Write(item + " ");
            }

Console.ReadKey();
        }
    }
}

输出

LINQ Syntax

LINQ混合语法

LINQ混合语法 is a combination of both Query and MethodSyntax.

LINQ Syntax

示例:现在,我们正在更改要求。首先,我们需要过滤列出值大于5的列表,然后我们需要计算总和。

using System;
usingSystem.Collections.Generic;
usingSystem. Linq;
usingSystem. Text;
usingSystem.Threading.Tasks;

namespace ConsoleApp1
{
classProgram
    {
staticvoid Main(string[] args)
        {
//Data Source
            ListintegerList = new List()
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };
//LINQ Query using Mixed Syntax
varMethodSyntax = (fromobjinintegerList
whereobj> 5
selectobj).Sum();
//Execution
Console.Write("Sum Is : " + MethodSyntax);

Console.ReadKey();
        }
    }
}

现在我们运行应用程序,它将显示总和为40,如下面控制台窗口的输出中所示。

输出

LINQ Syntax




祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

如何设计一个秒杀系统 -〔许令波〕

MySQL实战45讲 -〔林晓斌〕

Django快速开发实战 -〔吕召刚〕

恋爱必修课 -〔李一帆〕

Spring编程常见错误50例 -〔傅健〕

如何读懂一首诗 -〔王天博〕

业务开发算法50讲 -〔黄清昊〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

云时代的JVM原理与实战 -〔康杨〕

好记忆不如烂笔头。留下您的足迹吧 :)