Wednesday, 4 July 2018

005 LINQ to XML

XML is well known format to hold data. It is widely accepted all over the world. Formatting XML data depend upon the node ,attribute, elements ect . You can easily access , modify node , attribute , elements through programming languages. LINQ to XML is it functionality of.Net framework to query and modify the XML in memory.  LINQ to XML increase the productivity, developer need to write less code and it is compact. To  implement LINQ to XML, you need to load XML in memory first, then only you can modify or query in the XML document. The advantage of these approaches is, the process is integrated with languages and very lightweight, during the compilation of program , checking is done over the query and modification. After the query or notification is over , now you can save the XML document to physical file and you can do the necessary work with memory  XML file.  System.Xml.Linq  is the namespace  is used for XML manipulation.

Below Example Show 
1)A Simple select query ,return list of student.
2)A Simple select query with where condition,return list of student whose marks greater than 15
3)A Simple select query with where condition,return list of student whose Roll is 14.



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.Text;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
         class Program
        {
             static void Main(string[] args)
             {

               StringBuilder sb = new StringBuilder();
               sb.Append("");
               sb.Append("");
               sb.Append("John1");
               sb.Append("12");
               sb.Append("12.50");
               sb.Append("");

               sb.Append("");
               sb.Append("John2");
               sb.Append("13");
               sb.Append("13.50");
               sb.Append("");

               sb.Append("");
               sb.Append("John3");
               sb.Append("14");
               sb.Append("14.50");
               sb.Append("");

               sb.Append("");
               sb.Append("John4");
               sb.Append("14");
               sb.Append("14.50");
               sb.Append("");

               sb.Append("");
               sb.Append("John5");
               sb.Append("15");
               sb.Append("15.50");
               sb.Append("");

               sb.Append("");
               sb.Append("John6");
               sb.Append("16");
               sb.Append("16.50");
               sb.Append("");

               sb.Append("");
               sb.Append("John7");
               sb.Append("17");
               sb.Append("17.50");
               sb.Append("");
               sb.Append("");

       TextReader tr = new StringReader(sb.ToString());
       XDocument xdoc = XDocument.Load(tr);

       /******************LINQ to XML --Simple Select Query*******************/

       Console.WriteLine("*********************List of Student*******************");
       var lv1s = from lv1 in xdoc.Descendants("student")
                  select new
                  {

                  Name = lv1.Element("Name").Value,
                  Roll = lv1.Element("Roll").Value,
                  Marks = lv1.Element("Marks").Value
                 };

              foreach (var lv1 in lv1s)
              {

                 Console.WriteLine("Name = {0} , RollNumber = {1} ,Marks = {1} ",
                 lv1.Name, lv1.Roll, lv1.Marks);

              }

              Console.ReadKey();

        Console.WriteLine("*********************List of Student, Marks greated than 15***********");

            var lv1a = from lva in xdoc.Descendants("student")
                      where Convert.ToDecimal(lva.Element("Marks").Value) >= 15
                      select new
                      {

                         Name = lva.Element("Name").Value,
                         Roll = lva.Element("Roll").Value,
                         Marks = lva.Element("Marks").Value
                     };


             foreach (var lv1 in lv1a)
             {

                Console.WriteLine("Name = {0} , RollNumber = {1} ,Marks = {1} ",
                 lv1.Name, lv1.Roll, lv1.Marks);

             }
             Console.ReadKey();


Console.WriteLine("*********************List of Student, Filter by Roll=14***********");

          IEnumerable<XElement> stu = from el in xdoc.Elements("student")

          where (string)el.Element("Roll") == "14"

          select el;


           foreach (var stuElm in stu)
          {

               Console.WriteLine("Name = {0}", stuElm.Name);

          }
             Console.ReadKey();

         }

     }

}

Output :
*********************List of Student*******************
Name = John1 , RollNumber = 12 ,Marks = 12
Name = John2 , RollNumber = 13 ,Marks = 13
Name = John3 , RollNumber = 14 ,Marks = 14
Name = John4 , RollNumber = 14 ,Marks = 14
Name = John5 , RollNumber = 15 ,Marks = 15
Name = John6 , RollNumber = 16 ,Marks = 16
Name = John7 , RollNumber = 17 ,Marks = 17
*********************List of Student, Marks greated than 15***********
Name = John5 , RollNumber = 15 ,Marks = 15
Name = John6 , RollNumber = 16 ,Marks = 16
Name = John7 , RollNumber = 17 ,Marks = 17
*********************List of Student, Filter by Roll=14***********

Tuesday, 3 July 2018

004 LINQ to Object

LINQ offer query against object . You can query an object from in memory collection.Array, list, collections can be complex , and can hold data of a particular type of object. You can query on collection objects with LINQ.


              Before the introduction of LINQ , developer used  for loop to retrieve data from collections and write condition to filter the data. That was very hectic . LINQ introduced simple technique to retrieve the data buy simple writing declarative code that reduce effort and  clean, structure code with more accuracy and  faster than for loop.

Below is the example of LINQ to Object .The first example is of a simple query on a array.The second example is of a collection of a particular type of object.

Example



using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using System.Linq;
using System.Collections;


        namespace ConsoleApplication1
        {
                 class Program
                {
                      static void Main(string[] args)
                            {

                              string[] country = { "India", "Brazil", "China", "USA", "Pakistan", "South Afrika", "Japan", "Russia", "Poland", "Sweden" };


                                 /* Example 1 (Simple Select Statement) */

                                  var AllCountry = from c in country select c;

                                   Console.WriteLine("List of Country");

                                    Console.WriteLine("----------------");



                                   foreach (var c in AllCountry)
                                   {

                                            Console.WriteLine(c);

                                    }

                                    Console.ReadLine();



                                   /* Example 3 (LINQ on Array List) */

                                   ArrayList myList = new ArrayList();

                                   myList.Add(

                                    new student

                                   {

                                         name = "John",

                                         roll = 12,

                                          age = 9

                                   });

                                    myList.Add(

                                     new student

                                     {

                                        name = "John1",

                                        roll = 14,

                                         age = 8

                                       });

                                    myList.Add(

                                    new student

                                   {

                                          name = "John2",

                                          roll = 13,

                                          age = 7

                                      });

                                    myList.Add(

                                     new student

                                    {

                                         name = "John3",

                                         roll = 15,

                                          age = 8

                                    });


                             var query = from student s in myList

                              where s.age > 7

                                      select s;


                          Console.WriteLine("List of Student Over 7 Years of Age");

                          Console.WriteLine("--------------------------------------");



                          foreach (student c in query)
                         {

                                  Console.WriteLine("Name : " + c.name);

                                  Console.WriteLine("Roll : " + c.roll);

                                   Console.WriteLine("Age : " + c.age);

                                   Console.WriteLine("\n");

                           }
                            Console.ReadLine();

                  }

                 }

}

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

Output :

List of Country
----------------
India
Brazil
China
USA
Pakistan
South Afrika
Japan
Russia
Poland
Sweden

List of Student Over 7 Years of Age
--------------------------------------
Name : John
Roll : 12
Age : 9


Name : John1
Roll : 14
Age : 8


Name : John3
Roll : 15
Age : 8



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

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