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

How to implement query expression in LINQ

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

Share

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

This article mainly introduces LINQ how to achieve query expression, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Create a new page called Step1.aspx. Add a GridView control to the page, as follows:

City Names

Then in the background code file we will write the classic "hello world" LINQ example-including searching and sorting a list of strings:

Using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Query; public partial class Step1: System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {string [] cities = {"London", "Amsterdam", "San Francisco", "Las Vegas", & nbsp; "Boston", "Raleigh", "Chicago", "Charlestown", & nbsp "Helsinki", "Nice", "Dublin"}; GridView1.DataSource = from city in cities & nbsp; where city.Length > 4 & nbsp; orderby city & nbsp; select city.ToUpper (); GridView1.DataBind ();}

In the above example, I listed the names of a group of cities I visited from January to May this year. Then I use the LINQ query expression (query expression) to manipulate the array. This query expression returns all cities whose names are more than 4 characters, then sorts them by the letters of the city names and converts the names to uppercase.

The LINQ query expression returns the following type: the type of object selected by the IEnumerable- "select" clause determines the type here. Because "city" in the above example is a string, the result of type safety is a generic-based collection as follows:

IEnumerable result = from city in cities & nbsp; where city.Length > 4 & nbsp; orderby city & nbsp; select city.ToUpper ()

Because the ASP.NET control can be bound to any IEnumerable collection, we can easily bind the LINQ query results to GridView and then call the DataBind () method.

Note that in addition to the GridView control above, I can also use the

< asp:repeater>

< asp:datalist>

< asp:dropdownlist>

, or any other ASP.NET list control, which can be included with the product or developed by the developer In these examples, I only used the

< asp:gridview>

-but you can use any other control.

Thank you for reading this article carefully. I hope the article "how to implement query expressions in LINQ" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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