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's the use of LINQ anonymous types?

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

Share

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

This article is about the usefulness of LINQ anonymous types. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

LINQ Anonymous Type (Anonymous Types)

One of the other new C # and VB features that LINQ can take advantage of is support for LINQ anonymous types. This allows you to easily create and use inline type structures without explicitly declaring the object model, because types can be inferred from data initialization. This is useful when querying "custom configuration (custom shape)" data using LINQ.

For example, consider a scenario where you are working with a database with many properties or a strongly typed collection-but you only care about a few of them. It is more useful to return only the fields you need than to create and process the entire type. Let's create a new "step6.aspx" file to do the above:

Anonymous Type

In our background code file, we will write a LINQ query that uses LINQ anonymous types, as follows:

Using System; using System.Web.UI; using System.Query; public partial class Step6: System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {TravelOrganizer travel = new TravelOrganizer (); GridView1.DataSource = from location in travel.PlacesVisited & nbsp; orderby location.City & nbsp; select new {& nbsp; & nbsp;City = location.City, & nbsp; & nbsp;Distance = location.Distance & nbsp;}; GridView1.DataBind ();}}

Notice that instead of returning a "location" object from the select clause as above, we do this by creating a new LINQ anonymous type with City and Distance attributes. The types of these two properties are determined automatically based on the values they are assigned to when they are initialized, one of which is string and the other int.

Thank you for reading! This is the end of this article on "what's the use of LINQ anonymous types". 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