当前位置:网站首页>About LINQ statements
About LINQ statements
2022-04-21 18:09:00 【Mi Lin】
Linq A statement is a set of methods , Allows filtering of collections with minimal code 、 Sorting and grouping operations .Linq Represents language integration query . It is based on the integration of query functions into C# The name of the technology in the language . although Linq Methods are not language integrated , But there is also a declarative 、 Language integrated query snytax.
Array
The most basic enumerable items are arrays , It can store a fixed number of typed elements .
int[] numbers = new int[3] { 1, 2, 3};
In the above code, you create one that contains 3 An integer array of values , And use value 1、2 and 3 Initialize the array . Assign the array to int[] The variable of type is named numbers. Each element of the array can be accessed by indexing or enumerating values .
int fistNumber = numbers[0]; //1
foreach( int number in numbers){Console.WriteLine(number); // 1,2,3}
list
A common type of enumeration is list , Unlike arrays , The size of the list is variable .
var number = new list<int>{1,2,3};
In the example above , Created a list of integers , And use three values 1,2,3 Initialize it . Use keywords var To define the type of number . Use var, We let the compiler decide which type to use . When we will List<int> When an object of type is assigned to a number , The type of the variable will be List<int>. These value indexes can also be accessed, and they can also be enumerated .
var FirstNumber = number[0]; //1
froeach(var numbers in number){
Console.WriteLine(numbers); // 1,2,3
}
aggregate
The collection contains unique elements , for example ,HashSet Class provides high performance collection operations ( for example UnionWith、IntersectWith etc. )
var number = new HashSet<int>{1,2,3}
With the front 2 The difference between an array and a list is , You cannot access the elements of a collection through its index . There is no such concept as element index in hash set . however , You can still enumerate the values stored in the collection .
foreach( int number in numbers){Console.WriteLine(number); //1,2,3}
Although all enumerable types have different purposes and methods , But they have one thing in common . They all implement the same interface , namely IEnumerable<T>,Net in ,C# There is a declarative language structure in , Make it easy to handle enumeration items , Like the one above foreach loop .
Other precautions : Interface IEnumberable<T> It's a universal interface . This means that enumerations are strongly typed , But you can define specific types in your code . Strong typing means that it can only hold elements of the specified type ( Or you can implicitly convert elements of this type ).T It's the template type , Is the placement order of the specified type .
Type list <T> It's also a generic type . This list can be used for any type of (<T>). however , You have to choose a fixed type in the code . for example <int>( Shaping list ) yes IEnumerable<int>( Integer enumerable ) You can add... To the list int value . You can also add byte values to the list ( Because they can be automatically converted to int, Invisible transformation , Without losing ). however , You cannot add strings or long values to the list . The former cannot be replaced with an integer at all , The latter can be converted to an integer , But you may lose data , And it's never done automatically .
Filtering data
in many instances , Data sets must be filtered , A dataset is a collection of objects (IEnumerable<T>), A climbing predicate is an expression or function , You can decide whether to keep an item . If the predicate matches , Keep this item . without , You just try to drive it away . The following example : Keep only positive values from the list of numbers . The first way to filter data is to create a new empty list , Then enumerate the numbers , And add a positive value to the new list .


The above successfully created a filtering method KeepositiveNumbers, In this method , We can pass a list of arbitrary numbers as input , And just get a list of positive numbers . This method accepts any IEnumerable<int>, Therefore, the method is implemented using IEnumerable<T> Array of interfaces 、 Lists or other data structures . To be added ..
版权声明
本文为[Mi Lin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211801484210.html
边栏推荐
- Summary of mongodb user permissions
- 终于完成学生时代的梦想-制作掌机用单片机STM32手把手教你
- Graphic drawing in MATLAB
- 【acwing】165. 小猫爬山(DFS之剪枝)
- What happens when the user sends a request to execute the controller method
- Akismet plugin tutorial WordPress prevent spam filtering plugin
- 单片机diy作品鉴赏,初学者进来膜拜
- Thesis | attention based bidirectional long short term memory networks for relation classification
- Weekly recommended short video: the competition between enterprises is to see who can seize the first opportunity
- Download times remaining, link + email can be left
猜你喜欢

【Redis】 使用Redis优化省份展示数据不显示

【acwing】1118. 分成互质组 ***(DFS)

VS2019 visual studio 终端调试乱码问题

Thesis | attention based bidirectional long short term memory networks for relation classification

看完这篇教程,你将拥有自己的一个卫星(diy全程详解)

离散数学 命题逻辑

Porting openharmony and adding WiFi driver

上位机这样玩,才有意思!

【acwing】165. 小猫爬山(DFS之剪枝)

Download times remaining, link + email can be left
随机推荐
[刷力扣] 51-60题
单片机能做什么,你有什么有单片机或开源硬件做的有意思的作品吗
Logstash ~ output of logstash
Thesis | attention based bidirectional long short term memory networks for relation classification
Logstash ~ logstash one-stop tutorial (continuously updated)
How can the manufacturing industry save itself under the crisis of insufficient personnel and broken supply chain?
Why do infrastructure engineers prefer MySQL?
Logstash ~ logstash command line optional parameters
Build a short video platform and copy a demo of the login interface
[7:00 pm tonight] discussion on the development and application scenarios of metartc
封装的JDBC工具
2022超星学习通:媒体创意经济、走进东盟、职业生涯提升、音乐鉴赏、突发事件及自救
单片机diy作品鉴赏,初学者进来膜拜
JSON. Stringify() and JSON parse()
Google has been unable to collect the transformation target on the mobile terminal. What's the matter?
mysql8.0设置忽略大小写后无法启动
还剩下载次数,可留链接+邮箱
爬虫案例01
Part time jobs are higher than wages. In 2022, there are three sidelines with a monthly income of more than 10000
Appreciation of single chip microcomputer DIY works, beginners come in to worship