Overview of LINQ Data Source in ASP.NET

Discussion in 'ASP.NET' started by MinalS, Sep 9, 2015.

  1. The data source control enables user to use the LINQ in the ASP.NET web page. The text is used for accessing and changing the values.

    Properties of LinqDataSource are as listed below:
    1. AutoSort: The value states whether the control creates a Where clause dependent on the values defined in the WhereParameters.
    2. Adapter: It provides the adapter specific to the browser.
    3. ClientID: The server control identifier created by ASP.NET application
    4. Context: The HttpContext object associated with the particular server control
    5. DataItemContainer: The naming container is implementing the IDataItemContainer, the reference is provided
    6. DesignMode: Defines the control to be used on the design surface
    7. EnableTheming: Defines whether theme is supported by the control
    8. Events: A list of event handler delegates for the control
    9. ID: The identifier assigned to the server control
    10. InsertParameters: The parameters used for the insert operation are defined
    11. OrderBy: The fields used for ordering and accessing the data
    12. Select: The properties and the calculated values are defined
    Methods of LinqDataSource control are listed below:
    1. ApplyStyleSheetSkin: The styles present in the style sheet are applied to the cotnrol
    2. BuildProfileTree: The data about the server control is delivered to the Trace property while tracing is enabled for the web page
    3. ClearChildViewState: The view state information is deleted for the server child controls
    4. CreateControlCollection: A collection to store the child controls
    5. DataBind(): It binds the data source used for adding the server control and child controls
    6. EndRenderTracing: The design time tracing for the control is ended
    7. Focus: Used for setting focus to the control
    8. GetHashCode: The default hash function for the control is provided
    9. GetType: The Type of current instance is accessed
    10. Insert: An insert operation is performed
    11. Render: The server control is sent to the HtmlTextWriter object, it writes the content to be rendered to the client
    12. SaveViewState: The current view state for the control is saved

    Syntax for LinqDataSource control in ASP.NET



    Code:
    
    <asp: LinqDataSource ID=”string” AutoPage=”True | False” runat=”server” EnableInsert = “True | False” EnableDelete =”True | False” TableName = “string” Visible =”True | False” >
        <GroupByParameters/>
        <OrderByParameters/>
        <InsertByParameters/>
        <SelectParameters/>
        <WhereParameters/>
    </asp:LinqDataSource>
    

    Overview of LINQ



    LINQ provides a standard way to access the data from different data sources. The standard query is used for accessing data from data sources. There is no need for the user to understand the technology for data access.

    The query expression is used for accessing the data from different sources. User can perform operations like filter, group, sort, and join.

    The different steps to be performed for accessing the data using the LINQ are mentioned below:

    1) Define the data source

    In LINQ, user needs to define the data source from which the data is retrieved. The source can vary from an array, a XML file, or a collection.

    If the data source is an XML file, the source is defined as:

    Code:
    
    XElement Student = XElement.Load ( @”c:\studentdata.xml”);
    
    
    Once user has defined the data source, a query expression for accessing the data is defined.

    2) Creating a query

    The information to be accessed from the data source is defined in the query. The from and where clause are used for creating a query.

    Code:
    
    var query = from < type of the value returned> stud in Student 
    select stud;
    
    
    The from and select clause are defined in the query. The select clause defines the value types and form clause defines the range variable.

    3) Executing a query

    The query variable is used for storing the query in LINQ. The foreach loop is used for executing the query. The results are accessed using the LINQ query.

    Code:
    
    foreach ( var stud in query )
    {
        Response.Write ( stud );
    }
    
    
    The stud variable holds the value and the sequence is returned to the user.

    Example of LINQ



    Consider an example of Student data demonstrating the concept of LINQ query.

    1) Create an ASP.NET web application in Visual Studio

    2) Add the following code in the Student.cs class

    Code:
    
    public class Student
    {
        public int StudID { get; set; }
        public string StudName { get; set; }
        public int Marks { get; set; }
    
    public static List<Student> GetData()
    {
        List<Student> list1 = new List <Student> ();
        list1.Add( new Student { ID = “100”, StudName = “Gayatri”, Marks =”30” } );
    
        list1.Add( new Student { ID = “101”, StudName = “Raj”, Marks =”60” } );
    
        list1.Add( new Student { ID = “102”, StudName = “Rohan”, Marks =”70” } );
    
        list1.Add( new Student { ID = “103”, StudName = “Nitin”, Marks =”56” } );
    
        return list1;
    }
    }
    
    
    3) In the Page_Load event of the web page, add the following code

    Code:
    
    public partial class query1 : System.Web.UI.Page
    {
        protected void Page_Load ( object sender, EventArgs e)
        {
            List < Student> student = Student.GetData();
            var studname = from s in student select s.StudName;
    
            foreach ( var name in studname )
            lblName.Text += String.Format ( “{0} <br/>” , name );
        }
    }
    
    

    LINQ operators



    The different LINQ operators and their functionality are explained below.

    1) Ordering

    The data can be accessed and provided in a sorted way using the ordering query. The orderby clause is used. The data can be sorted in ascending or descending order.

    The following code snippet demonstrates the ordering of data using LINQ query.

    Code:
    
    var query = from <type of the value returned> stud in Student
                where stud.StudName == “Raj”
               orderby stud.StudName descending
               select stud;
    
    
    2) Filtering

    The filtering is used for accessing the data dependent on specific condition. The data matching the query is returned. The where clause is used for defining the condition and accessing the value.

    Code snippet to show filtering the data

    Code:
    
    var query = from <type of the value returned> stud in Student
                where stud.StudName == “Raj”
               select stud;
    
    
    3) Joining

    The data can be present in more than one table/object. The data to be accessed is present in more than one table/object. There must be a common field in both the tables used for joining the tables. The join clause is used for joining the tables.

    Code:
    
    var query = from <type of the value returned> stud in Student join
              <type of value returned> emp in Employee on stud.ID equals
            emp.ID
                select new { studName = stud.Name, Location = Emp.Location };
    
    
    Two array list Student and Employee are used as the data sources. They have ID as the common field, used for joining the tables.

    4) Grouping

    The group clause is used for accessing data and grouping it.

    Code:
    
    var query = from <type of the value returned> stud in Student
                group stud by stud.Name;
    
    
    The query generates the list of lists as the result. Every element is an object having a Key member and elements lists are grouped. The nested for loop is used for the iteration. The outer loop iterates for every group and inner loop over the group members.

    5) Let clause

    The let clause helps user define the variables and assign values to them. The calculated values are assigned to the result.

    Code:
    
    var studentinfo = from s in Student 
                let percentage = ( s.marks  / 100 )
                select new { Name = s.studID, Result = percentage } ;
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice