Monday, 9 July 2018

016 LINQ Sum


Sum() method return the calculated sum of numeric items in the collection .Below example is the example of Sum method of 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)
                      {
                             int[] marks = { 10, 20, 30, 40, 50, 60 };
                             int sumOfMarks = (from x in marks select x).Sum();
                             Console.WriteLine("The Sum is :" + sumOfMarks.ToString());
                             Console.ReadKey();
                      }
         }

}

//The Sum is :210
----------------------------------




Below example is the example of Sum method of Lambda query on a array.

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)
                {
                      int[] marks = { 10, 20, 30, 40, 50, 60 };
                           double sumOfMarks = marks.Sum(a=>a);

                           Console.WriteLine("The Sum is :" + sumOfMarks.ToString());
                           Console.ReadKey();
                }
      }
}

//The Sum is :210
----------------------------------------------------






Below example is the example of Sum method of LINQ query on a collections.
---------------------------------------------------
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)
                            {
                               IList<student> myList = new List<student>()
                              {
                                new student { name = "John1", roll = 121, age = 5 },
                                new student { name = "John2", roll = 122, age = 6 },
                                new student { name = "John3", roll = 123, age = 7 },
                                new student { name = "John4", roll = 124, age = 8 },
                                new student { name = "John5", roll = 125, age = 9 },
                                new student { name = "John6", roll = 126, age = 10 },
                                new student { name = "John7", roll = 127, age = 11 },
                                new student { name = "John8", roll = 128, age = 17 },
                                new student { name = "John9", roll = 129, age = 18 },
                                new student { name = "John10", roll = 1210, age = 19 },
                                new student { name = "John11", roll = 1211, age = 20 }
                              };

                             var sumOfAge = myList.Sum(a => a.age);

                             Console.WriteLine("The Sum is :" + sumOfAge.ToString());
                              Console.ReadKey();

                          }
            }
}

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







Below example is the example of Sum method of Lambda query on a collcetions.
---------------------------------------------------
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)
         {
                    IList<student> myList = new List<student>()
                   {
                         new student { name = "John1", roll = 121, age = 5 },
                         new student { name = "John2", roll = 122, age = 6 },
                         new student { name = "John3", roll = 123, age = 7 },
                         new student { name = "John4", roll = 124, age = 8 },
                         new student { name = "John5", roll = 125, age = 9 },
                         new student { name = "John6", roll = 126, age = 10 },
                         new student { name = "John7", roll = 127, age = 11 },
                         new student { name = "John8", roll = 128, age = 17 },
                         new student { name = "John9", roll = 129, age = 18 },
                        new student { name = "John10", roll = 1210, age = 19 },
                        new student { name = "John11", roll = 1211, age = 20 }
                };

                var sumOfAge = myList.Max(a => a.age);

                 Console.WriteLine("The Sum is :" + sumOfAge.ToString());
                  Console.ReadKey();

               }
     }

}

//The Maximum is :60
public class student
{
         public string name { get; set; }
         public int roll { get; set; }
        public int age { get; set; }
};

015 LINQ Skip

The Skip() method skips the specified number of element  from first element and returns rest of the elements.

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)
                          {
                             int[] marks = { 10, 20, 30, 40, 50, 60 };

                             var newList = marks.Skip(2);

                             foreach (var str in newList)
                            Console.WriteLine(str);

                            Console.ReadKey();
                       }
              }
}
//30
//40
//50
//60
-------------------------------------------- 

SkipWhile() method skip elements the till the specified condition meet . SkipWhile() returns a new collection that includes all the remaining elements that condition does not meet.
-------------------------------------------------------
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)
                     {
                      int[] marks = { 10, 20, 30, 40, 50, 60 };
                      var newList = marks.SkipWhile(s => s < 40); ;

                      foreach (var str in newList)
                      Console.WriteLine(str);

                      Console.ReadKey();
                 }
        }

}
//40
//50
//60
------------------------------------------------------



The Take()  method returns the specified number of elements from starting element to specified number of element.

----------------------------------------------------
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)
                 {
                    int[] marks = { 10, 20, 30, 40, 50, 60 };

                   var newList = marks.Take(2);

                   foreach (var str in newList)
                   Console.WriteLine(str);

                  Console.ReadKey();
             }
       }
}
//10
//20
----------------------------------


The TakeWhile() extension method returns elements of collection untill specified condition is true.
------------------------------------------
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)
                    {
                        int[] marks = { 10, 20, 30, 40, 50, 60 };

                       var newList = marks.TakeWhile(s => s) <40 font="">

                       foreach (var str in newList)
                       Console.WriteLine(str);

                       Console.ReadKey();
                 }
         }

}
//10
//20
//30

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













014 LINQ Nth Record


Finding N th record is very common query.It is asked during interview as well as useful during practical application.LINQ facilitate to find N th record of a collection.Below is the 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("*************LINQ nth record(Lamba Expression)***********/");


                int n = 3;
                var query = students.ElementAtOrDefault(n - 1);

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

                Console.ReadKey();

            }

       }

}

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



*************LINQ nth record(Lamba Expression)***********/
Name :John3 ,Age :14,Roll :125
-------------------------------------------------


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

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