Tuesday, 10 July 2018

022 LINQ OrderBy and OrderByDescending

A sorting operator arranges the elements of the collection in ascending or descending order.

Here is the example of sorting by name.The example shows that how sorting can be done ascending or descending order with LINQ query.


Example
-------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using System.Linq;
using System.Collections;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{

              class Program
              {

                  static void Main(string[] args)
                  {


                     var students = new student[]
                                {
                                    new student{name = "John1",roll=123,age=12},
                                    new student{name = "John2",roll=124,age=13},
                                    new student{name = "John3",roll=125,age=14},
                                    new student{name = "John4",roll=126,age=15},
                                    new student{name = "John5",roll=127,age=16},
                                    new student{name = "John6",roll=128,age=17},
                                    new student{name = "John7",roll=129,age=18}
                              };


/************Example of Order ***************************/


Console.WriteLine("*************Example of Order by Name***********/");

                var query1 = from s in students
                                       orderby s.name
                                       select s;

                           foreach (var obj in query1)
                          {

                               Console.WriteLine("Name :{0} ,Age :{1},Roll :{2}", obj.name, obj.age, obj.roll);

                          }




Console.WriteLine("*************Example of Order by Name Decending***********/");

             var query2 = from s in students

                                    orderby s.name descending

                                    select s;


                          foreach (var obj in query2)
                           {

                                       Console.WriteLine("Name :{0} ,Age :{1},Roll :{2}", obj.name, obj.age, obj.roll);

                          }
                       Console.ReadKey();

                       }

           }


}

public class student
{
            public string name { get; set; }
            public int roll { get; set; }
            public int age { get; set; }
}

Output :

*************Example of Order by Name***********/
Name :John1 ,Age :12,Roll :123
Name :John2 ,Age :13,Roll :124
Name :John3 ,Age :14,Roll :125
Name :John4 ,Age :15,Roll :126
Name :John5 ,Age :16,Roll :127
Name :John6 ,Age :17,Roll :128
Name :John7 ,Age :18,Roll :129



*************Example of Order by Name Decending***********/
Name :John7 ,Age :18,Roll :129
Name :John6 ,Age :17,Roll :128
Name :John5 ,Age :16,Roll :127
Name :John4 ,Age :15,Roll :126
Name :John3 ,Age :14,Roll :125
Name :John2 ,Age :13,Roll :124
Name :John1 ,Age :12,Roll :123

------------------------------------------------------------------------------------





Below is the example of sorting with Lambda expression.The query is sorted by name with Lambda expression both ascending or descending order.

Example
----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using System.Linq;
using System.Collections;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{

          class Program
          {

             static void Main(string[] args)
             {

                  var students = new student[]
                  {
                    new student{name = "John1",roll=123,age=12},
                    new student{name = "John2",roll=124,age=13},
                    new student{name = "John3",roll=125,age=14},
                    new student{name = "John4",roll=126,age=15},
                    new student{name = "John5",roll=127,age=16},
                    new student{name = "John6",roll=128,age=17},
                   new student{name = "John7",roll=129,age=18}
               };


Console.WriteLine("*************Example of Order by Name***********/");

              var query = students.OrderBy(a => a.name);

              foreach (var obj in query)
              {

             Console.WriteLine("Name :{0} ,Age :{1},Roll :{2}", obj.name, obj.age, obj.roll);

             }


Console.WriteLine("*************Example of Order by Name Decending***********/");
            var query2 = students.OrderByDescending(a => a.name);


           foreach (var obj in query2)
           {

                Console.WriteLine("Name :{0} ,Age :{1},Roll :{2}", obj.name, obj.age, obj.roll);

           }

       Console.ReadKey();

      }

   }


}


public class student
{
      public string name { get; set; }
      public int roll { get; set; }
      public int age { get; set; }
}


Output :
-------------------------------------------------------------------------------
*************Example of Order by Name***********/
Name :John1 ,Age :12,Roll :123
Name :John2 ,Age :13,Roll :124
Name :John3 ,Age :14,Roll :125
Name :John4 ,Age :15,Roll :126
Name :John5 ,Age :16,Roll :127
Name :John6 ,Age :17,Roll :128
Name :John7 ,Age :18,Roll :129
*************Example of Order by Name Decending***********/
Name :John7 ,Age :18,Roll :129
Name :John6 ,Age :17,Roll :128
Name :John5 ,Age :16,Roll :127
Name :John4 ,Age :15,Roll :126
Name :John3 ,Age :14,Roll :125
Name :John2 ,Age :13,Roll :124
Name :John1 ,Age :12,Roll :123
 

023 LINQ Question and Answer

1)What is LINQ ?

The full form of LINQ is language integrated query, that means the query can be written integrated with programming language. LINQ is a platform of standard query operator that is integrated with.Net 3.5 and upper version.
LINQ works with C#,VB.Net and other .Net languages.LINQ uniform syntax work with different data source like
DataSet , XML , SQL , Collection ect.

2)What is Lambda Expression ?


Lambda expression is an anonymous function or delegate  that create expression of query.Lambda expression uses symbol '=>'.Left side of this symbol is Input and right side is statement or expression. Here is the example of Lambda expression

Input=> expression or statement

3)How LINQ is beneficial over the Stored procedure ?

debug :LINQ is language integrated, that means you can debug your query during the debugging of  source code. Store procedure does not comes under language ,
Store procedure cannot be debug  form source code.

Types safe : LINQ is types  safe. Compile time checking are done during the compilation of program. Stored procedure cannot be check during the compile time , that may result run time exceptions.

4)Why select statement comes after form statement in LINQ query ?


.Net programming language like C @,VB.Net allow variable to declare first , then only you can use the variables. The form statement define the object of  LINQ,  now you can write select statement in your query.


Here is a example of a select statement



var query = from s in students
               order by s.name, s.age
               select s;
5)What is the extension of file ,  LINQ to SQL is used ?

The extension of file is "dbml". You need to add LINQ to SQL class first ,the filename will be your file name with extension "dbml". Now you can query over the "dbml" file.



6)What is DLINQ ?


It is nothing but LINQ to SQL.Namespace is used for this  purpose is System.data.dlinq.



7)What is the benefit of using LINQ over the DataSet ?


LINQ is strongly typed, compile time checking is done during compilation.error of querying during run time is minimized .


DataSet is filled with data from database containing one or multiple tables. LINQ query on DataSet by converting  as Enumerable(IEnumerable) and retrieve the data. No need to connect in database to get the required query result, you can directly query on DataSet by writing language integrated query which reduces effort and line of source code ,gives more accuracy.Here is an example of LINQ over DataSet.



var res = from exp in ds.Tables[0].AsEnumerable()

where exp.Field<string>("Name") == "John2"

                    select new
                    {

                              MyName = exp.Field<string>("Name"),

                              MyRoll = exp.Field<string>("RollNumber"),

                    };




8)What is query expression ?


query expression is a combination of queries instruction which is executed one by one to retrieve data from data source . For example filter ,short, uppercase, group by apply to the data source as query expression. Query expression Syntax are similar to the SQL syntax. Here is the syntax of a query expression.


IEnumerable Query =
                 from score in myData
                 where score > 85
                 orderby score descending
                 select score;

9)What are the basic step to execute LINQ query ?


2)First obtain a data source, the data source maybe collections, maybe LINQ to SQL file, maybe XML file.


3)Create a query.


4)Execute a query.


Below is the example of steps


var res = from exp in ds.Tables[0].AsEnumerable()

where exp.Field<string>("Name") == "John2"

select new
{

          MyName = exp.Field<string>("Name"),

         MyRoll = exp.Field<string>("RollNumber"),

};



foreach (var q in res)
{

Console.WriteLine("Name = {0} , RollNumber = {1} ",

q.MyName, q.MyRoll);

}



10)In which statement LINQ to SQL Query is executed ?


C Sharp :foreach
VB.Net : For Each


11)What is the difference between take and skip method of LINQ ?


"take" and "skip" method that provide some additional functionality on LINQ query.


"take" method return the element of an collection from starting to  position number supply to the take method.

  int[] marks = { 10, 20, 30, 40, 50, 60 };

    var newList = marks.Take(2);



"skip" method skip the number of element that supplied and and return the rest of the element from the collection.


 int[] marks = { 10, 20, 30, 40, 50, 60 };

 var newList = marks.Skip(2);

11)Which interface implement the standard query operator ?


Ans : IEnumerable & IQueryable



12)what are the different implementation of LINQ ?


There are various type of implementation of LINQ. LINQ can work with different kind of object and collections to provide faster and smoother  way to return desired result.



  • LINQ to SQL : LINQ to SQL work on relational database . This facility available from.Net framework 3.5 and upper version.

  • LINQ to DataSet : LINQ to DataSet convert datatable as IEnumerable. Filter , sort with standard of query is possible.

  • LINQ to XML : LINQ query on XML document.XML document is  used for storing data and later retrive from it. You can query on XML data.
  • LINQ to Object : LINQ on collection object.




বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...