In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "what is the relationship between LINQ queries and generic types". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
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:
1. When you create an instance of a generic collection class, such as List < (Of < (T >) >), you replace "T" with the type of object that the list will contain. For example, the list of strings is represented as List < string > and the list of Customer objects is represented as List < Customer >. 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 < string >, an error will occur at compile time. The reason generic collections are easy to use is that you do not have to perform run-time type casting.
2.IEnumerable < (Of < (T >) >) is an interface through which generic collection classes can be enumerated using foreach statements. Generic collection classes support IEnumerable < (Of < (T >) >), just as non-generic collection classes (such as ArrayList) support IEnumerable.
3.LINQ query variables are typed as IEnumerable < (Of < (T >) >) or as derived types, such as IQueryable < (Of < (T >) >). When you see a query variable typed as IEnumerable < Customer >, this only means that when the query is executed, the query will generate a sequence of zero or more Customer objects.
Use of LINQ queries:
IEnumerable < Customer > customerQuery = from cust in customers where cust.City = = "London" select cust; foreach (Customer customer in customerQuery) {Console.WriteLine (customer.LastName + "," + customer.FirstName);}
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, I suggest that if you use var, you should be aware that this may make your code more difficult for others to understand.
To write a query effectively, you should understand how the variable types in the complete query operation are all related to each other. If you understand these relationships, you can more easily understand the LINQ examples and code examples in the documentation. In addition, you can learn about the background operations when using var to implicitly type variables.
LINQ query operations are strongly typed in the data source, the query itself, and in query execution. The type of variable in the query must be compatible with the type of element in the data source and the type of iterative variable in the foreach statement. This strong typing ensures that type errors are caught at compile time so that they can be corrected before the user encounters them.
To demonstrate these type relationships, most of the following examples use explicit types for all variables.
Some questions about LINQ queries:
The following figure shows a LINQ to Objects query operation that does not perform a transformation on the data. The source contains a sequence of strings, and the query output is also a sequence of strings.
1. The type parameter of the data source determines the type of scope variable.
two。 The type of object you select determines the type of query variable. The name here is a string. Therefore, the query variable is an IEnumerable < string >.
3. Iterate through the query variables in the foreach statement. Because the query variable is a sequence of strings, the iterative variable is also a string.
The following figure shows a LINQ to SQL query operation that performs a simple transformation on the data. The query takes a sequence of Customer objects as input and selects only the Name attribute in the result. Because Name is a string, the query generates a sequence of strings as output.
1. The type parameter of the data source determines the type of scope variable.
The 2.select statement returns the Name property instead of the full Customer object. Because Name is a string, the type parameter of custNameQuery is string, not Customer.
3. Because custNameQuery is a sequence of strings, the iteration variable of the foreach loop must also be string.
The following figure shows a slightly more complex transformation. The select statement returns an anonymous type that captures only two members of the original Customer object.
1. The type parameter of the data source is always the type of the range variable in the query.
two。 Because the select statement generates anonymous types, query variables must be implicitly typed using var.
3. Because the type of query variable is implicit, iterative variables in the foreach loop must also be implicit.
4. Although you should understand the type relationships in query operations, you can also choose to have the compiler do all the work for you. The keyword var can be used to query any local variable in an operation. The following figure is completely equivalent to the second example discussed earlier. The difference is that the compiler will provide strong typing for each variable in the query operation:
That's all for "what is the relationship between LINQ queries and generic types". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.