Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a System.LINQ assembly

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is a System.LINQ assembly". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what a System.LINQ assembly is.

LINQ, or Language Integrated Query. Many people are familiar with the SQL language, and it is very convenient to use it to operate the database. Now you can use the same syntax to manipulate all kinds of data, including arrays, files, databases, etc., in Cnotify 3.0.

Let's look at an example:

Class Program... {static void Main (string [] args)... {string [] names=... {"Everett", "Albert", "George", "Harris", "David"}; var items=from name in names where name.Length > = 6 orderby name select name.ToUpper (); foreach (var item in items) Console.WriteLine (item);}}

If you are familiar with the above words such as from,where,orderby, we can easily query some arrays like querying a database. In fact, the above expression is equivalent to the following:

Var items=names.Where (name= > name.Length > = 6).

OrderBy (name= > name) .Select (name= > name.ToUpper ())

So why can we apply these methods to arrays? there are no such methods on arrays? Recall what we talked about, the extension method, and yes, we see the opportunity to use the extension method here. And we also see that the application of Lambda expressions makes the sentence more concise and easy to understand. Where are these extension methods defined? We can see this definition in the System.LINQ assembly:

Namespace System.LINQ... {public static class Enumerable... {public static IEnumerable Where (this IEnumerable source, Func predicate)... {foreach (T item in source) if (predicate (item)) yield return item;}

Here we can clearly see many this keywords, which are the hallmarks of the extension method. If we are not familiar with Lambda expressions, the above LINQ statement can be further transformed into the form of a delegate.

Func filter = delegate (string s)... {return s.Length > = 6;}; Func extract = delegate (string s)... {return s;}; Func project = delegate (string s)... {return s.ToUpper ();}; var items = names.Where (filter) .OrderBy (extract) .Select (project)

There is a question, we will notice that items used var as its type, so did you ask what type this var is? If we take a closer look at the System.LINQ assembly, we will see that the extension method returns the IEnumerable type, a generic interface, and yes, var is the generic interface. Another problem is that when names satisfies what conditions, we can apply LINQ expressions to query. This needs to be carefully observed from that assembly. We will find that there is a source parameter, and according to the syntax of the extension method, we know that this source parameter is the object that called the method. Then we can infer that this object should be able to be converted to IEnumerable. So what types can be converted successfully? Obviously, as long as you implement the generic interface IEnumerable, the type can be converted. Such as array types, List, and so on. When we can also define the type ourselves, as long as we implement this interface, we can manipulate the type with LINQ.

Thank you for your reading, the above is the content of "what is the System.LINQ assembly", after the study of this article, I believe you have a deeper understanding of what the System.LINQ assembly is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report