LINQ to Array


Query writing in LINQ

LINQ allows us to write query against all data whether it comes from array, database, XML etc. This is the best feature of LINQ.
Before going deep into LINQ lets run some basic queries in LINQ using arrays and try to understand the syntax of LINQ
The namespace which is used for these queries is as follows:-
using System.Linq;

Example of LINQ Queries

Let’s take the string array which contains the name of student as follows:-
  

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //declare the array
        string[] viastudy_student = new string[] { "durgesh", "shallu", "duggu", "bittu" };
       
        //quey using LINQ syntax
        IEnumerable<string> str_res = from res in viastudy_student
                                      select res;

        //access the element and print in the screen
        foreach (string final_res in str_res)
        {
            //show data with some spaces
            Response.Write(final_res+"   ");
       
        }
    }
}    
The output of this code as follows:-

Where Clause in LINQ

We can also filter data using where clause. Some queries and answer as follows:- Show the names which length are 4 then we will write the query as follows:-
  
IEnumerable<string> str_res = from res in viastudy_student
           where res.Length==4
           select res;  
Show the name which has the word “sh”
  
IEnumerable<string> str_res = from res in viastudy_student
           where res.Contains("sh")
           select res;   

order by in linq Query

If we want to sort the data then we use the order by in the query as follows:-
  
IEnumerable<string> str_res = from res in viastudy_student
              where res.Contains("sh")
           orderby res
           select res;  
By default it will sort in ascending order.
If we want to sort in descending order then we will write the query as follows:-
  
  IEnumerable<string> str_res = from res in viastudy_student
           where res.Contains("sh")
           orderby res descending
           select res; 

Customization of linq output

We can also customize the output using query in LINQ.
For Example
If you want to show the data in upper case then we customize the output as follows:-
  
IEnumerable<string> str_res = from res in viastudy_student
                                      where res.Contains("sh")
                                      orderby res descending
                                      select res.ToUpper();
  
  

Show Data in Gridview using linq

We can also show this data in gridview. The code is as follows:-
  
  using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //declare the array
               string[] viastudy_student = new string[] { "durgesh", "shallu", "duggu", "bittu" };

       
        //quey using LINQ syntax
        IEnumerable<string> str_res = from res in viastudy_student
  where res.Contains("sh")
  orderby res descending
  select res.ToUpper();

        GridView1.DataSource = str_res;
        GridView1.DataBind();
    }
}
  
  

How to Project Output

we can also project output in another form and can generate new results.
For Example
Let’s take the following array in which i want to show the ceiling values from the result
  
  // project elements
        decimal[] ar = { 2.5m, 3.8m, 4.9m, 5.1m, 6.4m };

        IEnumerable<decimal> res = from m in ar
                              
                               select Math.Ceiling(m);

        foreach (decimal p in res)
        {
            Response.Write(p + "<br>");

        }    
It will show the following output


No comments:

Post a Comment