Linq to Objects, linqtoobjects
Directory
Preface
Series of articles
Linq to objects
Summary
Preface
The previous article introduced the content of the latency loading feature of linq. From this article, we will introduce the content of the content such as linq to Objects, linq to xml, and linq to SQL.
Series of articles
A preliminary understanding of Lambda expressions in Linq
Lambda of Linq (advanced tutorial)
Implicit type, automatic attribute, initializer, and Anonymous class of Linq
Extended method of Linq
First Appearance of Expression of Linq
Expression IN Linq (advanced tutorial)
Expression of Linq (common Expression types)
Common keywords of Linq
Latency loading of Linq
Linq to objects
The term "LINQ to Objects" refers to the direct use of LINQ queries for any IEnumerable or IEnumerable <T> set, without the need to use intermediate LINQ providers or APIs, for example, LINQ to SQL [LINQ to SQL] Or LINQ to XML. You can use LINQ to query any enumerated sets, such as List <T>, Array, or Dictionary <TKey, TValue>. The set can be a user-defined set or a set returned by the. NET Framework API.
Basically, "LINQ to Objects" represents a new method for processing a set. Using the old method, you must write a complex foreach loop that specifies how to retrieve data from the set. With the LINQ method, you only need to write declarative code that describes the content to be retrieved.
In addition, compared with the traditional foreach loop, LINQ query has three major advantages:
1. They are more concise and easier to read, especially when filtering multiple conditions.
2. They use the least application code to provide powerful filtering, sorting, and grouping functions.
3. You do not need to modify them or just need to make small changes to port them to other data sources.
----- MSDN
In the previous article, use linq to query ArrayList.
In addition, it is very convenient to query data in the string, reflection, and file directory operations. In the following example, query all files under a directory named "extension .rar.
1 using System. IO; 2 using System. linq; 3 namespace Wolfy. linq2ObjectsDemo 4 {5 class Program 6 {7 static void Main (string [] args) 8 {9 var fileNames = from n in GetFileNames (@ "F :\",". rar ") 10 select Path. getFileName (n); 11 foreach (var item in fileNames) 12 {13 Console. writeLine (item); 14} 15 Console. read (); 16} 17 /// <summary> 18 // get the file name path 19 /// </summary> 20 /// <param name = "strPath"> </param> 21 /// <param name = "strExtention"> </param> 22 static IEnumerable <string> GetFileNames (string strPath, string strExtention) 23 {24 DirectoryInfo dir = new DirectoryInfo (strPath); 25 IEnumerable <System. IO. fileInfo> files = dir. getFiles ("*. * ", SearchOption. allDirectories); 26 // linq query 27 var result = from file in files28 where file. extension = strExtention29 select file. fullName; 30 return result; 31} 32} 33}
Output
Summary
The content of linq to objects is relatively simple. Maybe you have been using it for a long time in the project, so I will not talk about it more. For more information, see the following references.
Reference
Https://msdn.microsoft.com/zh-cn/library/cc981895.aspx
Https://msdn.microsoft.com/zh-cn/library/bb546159.aspx
Https://msdn.microsoft.com/zh-cn/library/bb397937.aspx