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

Example Analysis of Linq Source elements

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

Share

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

This article shares with you the content of a sample analysis of Linq source elements. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Linq has a lot to learn, and here we focus on selecting a subset of each Linq source element, including performing operations on Linq source elements.

Select a subset of each Linq source element

There are two main ways to select a subset of each Linq source element in the source sequence:

1. To select only one member of the Linq source element, use the dot operation. In the following example, assume that the Customer object contains several public properties, including a string named City. When this query is executed, the query generates a string output sequence.

Var query = from cust in Customers select cust.City

2. To create an element that contains multiple attributes of an Linq source element, you can use an object initializer with named objects or anonymous types. The following example shows how to use anonymous types to encapsulate two attributes of each Customer element:

Var query = from cust in Customer select new {Name = cust.Name, City = cust.City}

Perform actions on Linq source elements

The output sequence may not contain any elements or element attributes of the source sequence. The output may be a sequence of values calculated by using the Linq source element as an input parameter. When you execute the following simple query, this query outputs a sequence of strings that represent the calculation based on the source sequence of elements of type double.

Class FormatQuery {static void Main () {/ / Data source. Double [] radii = {1,2,3}; / / Query. IEnumerable query = from rad in radii select String.Format ("Area = {0}", (rad * rad) * 3.14); / / Query execution. Foreach (string s in query) Console.WriteLine (s); / / Keep the console open in debug mode. Console.WriteLine ("Press any key to exit."); Console.ReadKey ();}} Thank you for reading! This is the end of this article on "sample Analysis of Linq Source elements". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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