How to implement query expression in LINQ
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!