ASP.NET And LINQ Contd..

text zoom

ASP.NET And LINQ Contd..


The belwo LINQ example will  search and order a list of strings:











using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;


public partial class _Default : System.Web.UI.Page

     {

        protected void Page_Load(object sender, EventArgs e)

         {

           string[] cities = { "London", "Amsterdam", "San Francisco", "Las Vegas",

            "Boston", "Raleigh", "Chicago", "Charlestown",

            "Helsinki", "Nice", "Dublin" };


              GridView1.DataSource = from city in cities

             where city.Length > 4

             orderby city

             select city.ToUpper();


               GridView1.DataBind();

          }

    }



 


I’ve created an array of strings listing the cities I’ve visited from Jan->May of this year.   I’m then using a LINQ query expression against the array.  This query expression returns all cities where the city name is greater than 4 characters, and orders the result in alphabetical order and transforms those city names into upper case.


LINQ queries return results of type: IEnumerable<T> -- where <T> is determined by the object type of the “select” clause.  In the above sample “city” is a string, so the type-safe result is a generics based collection like so:


Output:


                    

Aspnet Related Tutorials

...more

New Aspnet Resources

...more

Copyright © 2013 VisualBuilder. All rights reserved