9:32 PM

How to use LINQ query in XML data souce

Posted by vijay



Here I share the usage of LINQ in xml.Normally xml has been to store the data like a database. By using LINQ query we can easily search or retrieve the data from XML.
In a previous article I explained, How LINQ query used in List Collection. For your reference I have mention URL as below.


Now here I explain how to use LINQ query on XML
Take xml file like as structure as below………

Stud.xml

<students>
<student Group="A">
<name>raju</name>
<class>10th</class>
</student>
<student Group="A">
<name>raju</name>
<class>10th</class>
</student>
<student Group="B">
<name>raju</name>
<class>5th</class>
</student>
<student Group="C">
<name>rahman</name>
<class>5th</class>
</student>
<student Group="A">
<name>sekhar</name>
<class>7th</class>
</student>
<student Group="B">
<name>rahuk</name>
<class>8th</class>
</student>
</s</students>

First we need to add namespace to use XML class in LINQ.
using System.Xml.Linq;
then we need to use XDOCUMENT class to load XML file to apply LINQ query.
In First query I am going to search the name contains “Raju” bu using LINQ query


XDocument xd=XDocument.Load(Server .MapPath ("stud.xml"));
        var xmldata = from xm in xd.Descendants("student") where xm.Element ("name").Value .Equals ("raju")
                      select new
                      {
                          name = xm.Element("name").Value,
                          studclass = xm.Element("class").Value
                      };
        foreach (var post in xmldata)
        {
            Response.Write(post.name);
            Response.Write(post.studclass);
        }

Output:
1raju10th
raju10th
raju5th

In a second process now we can filter by group…
var xmldata1 = from xm1 in xd.Descendants("student")
                       where xm1.Attribute("Group").Value.Equals("A")
                       select new
                       {
                           name = xm1.Element("name").Value,
                           studclass = xm1.Element("class").Value
                       };

        foreach (var post1 in xmldata1)
        {
            Response.Write(post1.name);
            Response.Write(post1.studclass + "</br>" );
        }

Output:
raju10th
raju10th
sekhar7th

I hope now you have got an idea to LINQ in xml

4:48 AM

How to use LINQ in List collection

Posted by vijay



Linq is stands for Language integrated query, its Microsoft DOTNET framework model, start from the framework 3.5
Linq enables you to query and manipulate data independently of data source. Data source may be the sql or dataset or XML
Linq encapsulates generics and it’s provided full safety and makes code more readable.
Linq supports the following providers,

  •  Linq to SQL
  • Linq to dataset
  • Linq to objects
  • Linq to XML

LINQ to objects:

By using this LINQ we must add the namespace using System.Linq;
1) First we need to create Generics List with set of strings, from the string we need to find the string start with letter of “A” using LINQ

List<string> lststr = new List<string>();
        lst.Add("vijay");
        lst.Add("anand");
        lst.Add("arun");
        lst.Add("ahamed");
        lst.Add("shahul");

        var r= from element in lst where element.StartsWith ("a")    select element;

        foreach (var r1 in r)
        {
            lststr.Add(r1.ToString());
            Response.Write("</br>" + r1);
        }


Output:
anand
arun
ahamed




2) In this process we create list of string array, from this array we must find the first array of the string using LINQ

   List<string[]> lst1 = new List<string[]>();

        string[] a = { "4", "8", "9" };
        string[] b = { "1", "3", "7" };
        string[] c = { "14", "18", "19" };
        string[] d = { "24", "28", "29" };
        lst1.Add(a);
        lst1.Add(b);
        lst1.Add(c);
        lst1.Add(d);

       var coll = from int1 in lst1 where int1[0].Equals("1") select int1;

        foreach (var r2 in coll)
        {
            Response.Write("</br>" + r2[0] + r2[1] + r2[2]);
        }

Output:
137

3) We create integer array then we start the array using LINQ

int[] a1 = { 2, 1, 4, 56 };

       
        var col2 = from int2 in a1 orderby int2 descending select int2;

        foreach (var r3 in col2)
        {
            Response.Write("</br>" + r3);
        }

Output:
56
4
2

I hope you have understood how to use LINQ in generics list……….