In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you the example analysis of LINQ query based on generic types, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
LINQ queries are based on generic types, which were introduced in version 2.0 of the .NET Framework. You don't need to know much about generics to start writing queries. However, you may need to understand two basic concepts:
◆ when you create an instance of a generic collection class, such as List, you replace "T" with the type of object that the list will contain. For example, a list of strings is represented as a list of List,Customer objects as a List. Generic lists are strongly typed and provide more benefits than storing their elements as collections of Object. If you try to add Customer to List, an error occurs at compile time. The reason generic collections are easy to use is that you do not have to perform run-time type casting.
◆ IEnumerable) is an interface through which you can enumerate generic collection classes using foreach statements. Generic collection classes support IEnumerable), just as non-generic collection classes such as ArrayList support IEnumerable.
IEnumerable variables in LINQ queries
The LINQ query variable is typed as IEnumerable) or a derived type, such as IQueryable). When you see a query variable typed as IEnumerable, this only means that when the query is executed, the query will generate a sequence of zero or more Customer objects.
IEnumerable customerQuery = from cust in customers where cust.City = = "London" select cust; foreach (Customer customer in customerQuery) {Console.WriteLine (customer.LastName + "," + customer.FirstName);}
Let the compiler handle generic type declarations
If you prefer, you can use the var keyword to avoid using generic syntax. The var keyword instructs the compiler to infer the type of query variable by looking at the data source specified in the from clause. The following example generates the same compiled code as the previous example:
Var customerQuery2 = from cust in customers where cust.City = = "London" select cust; foreach (var customer in customerQuery2) {Console.WriteLine (customer.LastName + "," + customer.FirstName);}
The var keyword is useful when the type of a variable is obvious or it is not important to explicitly specify nested generic types, such as those generated by a group query. In general, we recommend that if you use var, be aware that this may make your code more difficult for others to understand.
The above is all the content of the article "sample Analysis of LINQ queries based on generic types". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.