Saturday, December 31, 2011

Difference between Equal method and Equal to operator (==))

Equality comparison in C# is of two different kinds:-

1)       Value equality and
2)       reference equality

1) Value equality normally compares two objects by value.  

For ex:-

val1 and val2 are two integer variables having equal value as 10.

int val1 = 10;
int val2 = 10;
Console.WriteLine(val1.Equals(val2));
Console.WriteLine(val1 == val2);
Console.ReadLine();

Output
--------
True
True

Because the value equality is based on object values, the Output using the Equal method and Equal operator (==) is True.

2) Reference equality compares two objects by reference and not by value.

If there are two objects obj1 and obj2 which are referring to the obj3, then the reference equality will return true.

For ex:-

I have create a sample class
public class Test
{
   public Test()
   { }
}

Herein creating an object of Test class.
Test objTest = new Test();

Example 1:-

Now two different objects objT and objT1 is created which are referring to the objTest object.
Object objT = objTest;
Object objT1 = objTest;

Console.WriteLine(objT.Equals(objT1));
Console.WriteLine(objT == objT1);
Console.ReadLine();

Output
--------
True
True

Example 2:-

In this example, two different objects objT and objT1 are created of the Test class.

Object objOne = new Test();
Object objTwo = new Test();

Console.WriteLine(objOne.Equals(objTwo));
Console.WriteLine(objOne == objTwo);

Output
--------
False
False

objOne and objTwo are two objects of the same class Test but they are not pointing or referring to the same object, that’s the reason the reference equality will return False for reference comparison.


Happy Coding.......

Saturday, December 17, 2011

Converting multiple rows of a single column into single row


COALESCE (Transact-SQL):-

 MSDN definition:- Returns the first nonnull expression among its arguments.

 Syntax

COALESCE ( expression [ ,...n ] )

ISNULL and COALESCE though equivalent, can behave differently. An expression involving ISNULL with non-null parameters is considered to be NOT NULL, while expressions involving COALESCE with non-null parameters is considered to be NULL.

In today's example, we will be finding a way to convert mutliple rows of a single column into a delimited string value.

For example my sample table (tblEmployee) data in SQL Server is as follows (in tabular format):-

ID
Name
Department
Age

1
John Smith
Software
32
2
Steve Smith
Finance
35
3
Pradeep Pavaluru
Marketing Division
29

4
John Leo
Software
40

I want to combine all the names for the given employee records as a delimited string. For achieving the same i will be using COALESCE function.


declare @ReturnName varchar(max)

Select@ReturnName= COALESCE(@ReturnName + ',', '')+[Employee Name]

fromtblEmployee

print 'Employees Name= '''+@ReturnName+''''

Output:
Employees Name= 'John Smith,Steve Smith,Pradeep Pavaluru,John Leo'

Happy Coding.....



Friday, October 21, 2011

Adding new nodes into XML document using LINQ

Today example using LINQ with xml, will be used to show example to add new elements in the XML document and to update the existing values of the element nodes.

Using the .Net earlier classes XmlDocument, XmlTextReader,XPathNavigator etc, bulky code was required to traverse through the XML nodes and finding out the node we wanted to modify. But with the help of LINQ, traversing through the XML document is really easy.

Basic definitions:-

a) XDocument class - Represents an XML document.
b) XElement class- Represents an XML element.
c) XDocument.Load method - Creates a new XDocument from a file specified by a URI, 
    from an TextReader, or from an XmlReader.

Lets start the example to see how modifying and addition of nodes can be achieved with the help of LINQ.

Step 1:-

From my previous example i have copied here the same XML document customer.xml file, which contains the details about the customer. Ex:- Name, CompanyName, JobTitle etc.



<?xml version="1.0" encoding="utf-8" ?>
<Customers>
  <Customer Id="1">
    <Name>B. K. Mishra</Name>
    <CompanyName>FIC Limited</CompanyName>
    <JobTitle>Sales Representative</JobTitle>
    <Address>1st Block, 19th cross,sector 7</Address>
    <City>Bangalore</City>
    <Country>India</Country>
    <Fax>080-98765457</Fax>
    <EmailId>B.k.mishra@fic.com</EmailId>
  </Customer>
  <Customer Id="2">
    <Name>A. K. Mitra</Name>
    <CompanyName>Sourcesoft Ltd</CompanyName>
    <JobTitle>Maketing Manager</JobTitle>
    <Address>Building no-10, second floor,1st block</Address>
    <City>Delhi</City>
    <Country>India</Country>
    <Fax>011-23456789</Fax>
    <EmailId>AKMitra@sourcesoft.com</EmailId>
  </Customer>
  <Customer Id="3">
    <Name>S. Reddy</Name>
    <CompanyName>Edge solution</CompanyName>
    <JobTitle>Sales Representative</JobTitle>
    <Address>Raheja arcade,3rd floor, sector 1</Address>
    <City>Bangalore</City>
    <Country>India</Country>
    <Fax>080-22224455</Fax>
    <EmailId>SReddy@EdgeSol.com</EmailId>
  </Customer>
  <Customer Id="4">
    <Name>K. Manakotte</Name>
    <CompanyName>Info Ltd</CompanyName>
    <JobTitle>Marketing Manager</JobTitle>
    <Address>Citi tower, MG Road</Address>
    <City>Bangalore</City>
    <Country>India</Country>
    <Fax>080-20224554</Fax>
    <EmailId>k.manakotte@info.com</EmailId>
  </Customer>
</Customers>

Step 2:-

Now i will be writing the code in code behind file to load the xml document and then to add element to the existing xml tree.

XDocument xDoc = XDocument.Load("Customers.xml");
//retrieving the maximum value of Id attribute from the customer node 
var maxID = (from p in xDoc.Descendants("Customer")
                        select p.Attribute("Id").Value).Max();

//incrementing the ID value by 1
int newID = Convert.ToInt16(maxID)+1;

//creating a new customer element with Id attribute having newly created Id value
// then creating all the child elementes with respected value to each node
XElement xeCustomer = new XElement("Customer"new XAttribute("Id", newID + 1), 
                                                                   new XElement("Name""Stephen Smith"),
                                                                   new XElement("CompanyName""Nous System"),
                                                                   new XElement("JobTitle""Marketing Manager"), 
                                                                   new XElement("Address""Sky arcade,Karol Bagh"), 
                                                                   new XElement("City""Delhi"),
                                                                   new XElement("Country""India"), 
                                                                   new XElement("Fax""011-23455555"), 
                                                                   new XElement("EmailId""steve.smith@nous.com"));

//adding the newly created XElement to the xDoc object parent xml tree
xDoc.Element("Customers").Add(xeCustomer);

//Retrieving all the customer details from the xDoc
var customerList = from cust in xDoc.Descendants("Customer")
                                 select new
                                 {
                                     CustomerName = cust.Element("Name").Value,
                                     JobTitle = cust.Element("JobTitle").Value,
                                     Fax = cust.Element("Fax").Value,
                                     EmailId = cust.Element("EmailId").Value
                                 };

//displaying the customer details in the 
foreach (var cust in customerList)
{
    Console.WriteLine(cust.CustomerName + ";" + cust.JobTitle + ";" + cust.Fax + ";" + 
                                     cust.EmailId);
}

Output

After creation of new elements, the xDoc object can be passed to any database for record creation or can be used to write Xml document into a new file.

I hope this example has served the purpose of giving an idea on how to add new elements into the existing xml document using LINQ.

Happy coding....

Thursday, October 20, 2011

Reading XML values using LINQ

Today i will be using examples to show the ways to read XML nodes and attributes value using LINQ. Everyone is aware of the powerful features LINQ provides, and the best thing about LINQ is simple and easy way to use it.

Step 1:-

I have created a customer.xml file, which contains the details about the customer. Ex:- Name, CompanyName, JobTitle etc.

<?xml version="1.0" encoding="utf-8" ?>
<Customers>
  <Customer Id="1">
    <Name>B. K. Mishra</Name>
    <CompanyName>FIC Limited</CompanyName>
    <JobTitle>Sales Representative</JobTitle>
    <Address>1st Block, 19th cross,sector 7</Address>
    <City>Bangalore</City>
    <Country>India</Country>
    <Fax>080-98765457</Fax>
    <EmailId>B.k.mishra@fic.com</EmailId>
  </Customer>
  <Customer Id="2">
    <Name>A. K. Mitra</Name>
    <CompanyName>Sourcesoft Ltd</CompanyName>
    <JobTitle>Maketing Manager</JobTitle>
    <Address>Building no-10, second floor,1st block</Address>
    <City>Delhi</City>
    <Country>India</Country>
    <Fax>011-23456789</Fax>
    <EmailId>AKMitra@sourcesoft.com</EmailId>
  </Customer>
  <Customer Id="3">
    <Name>S. Reddy</Name>
    <CompanyName>Edge solution</CompanyName>
    <JobTitle>Sales Representative</JobTitle>
    <Address>Raheja arcade,3rd floor, sector 1</Address>
    <City>Bangalore</City>
    <Country>India</Country>
    <Fax>080-22224455</Fax>
    <EmailId>SReddy@EdgeSol.com</EmailId>
  </Customer>
  <Customer Id="4">
    <Name>K. Manakotte</Name>
    <CompanyName>Info Ltd</CompanyName>
    <JobTitle>Marketing Manager</JobTitle>
    <Address>Citi tower, MG Road</Address>
    <City>Bangalore</City>
    <Country>India</Country>
    <Fax>080-20224554</Fax>
    <EmailId>k.manakotte@info.com</EmailId>
  </Customer>
</Customers>

Step 2:

XDocument class represents a XML document. XDocument.Load method Creates a new System.Xml.Linq.XDocument object from a file.

(i) Following query will be used to retrieve all the customer details from the xml document
    
    //retrieving all the customer details
    var customerList =from cust in xDoc.Descendants("Customer")
                      select cust;
    foreach (var cust in customerList)
    {
      //writing output to the console output window
      Console.WriteLine(cust.Element("Name").Value + ";" +  
                        cust.Element("CompanyName").Value+";"+cust.Element("JobTitle").Value  
                        + ";"+cust.Element("Fax").Value + ";" +cust.Element("EmailId").Value
                       );
    }
  
  Output

(ii) In this query anonymous class will be used to retrieve all the customer details from the xml document.
     
     "Anonymous types typically are used in the select clause of a query expression to return a subset of the
   properties from each object in the source sequence."
     
     If we do not specify member names in the anonymous type, the compiler gives the 
     anonymous type members the same name as the property being used to initialize them.

    Using Anonymous type, custom name can be assigned to the properties to select values.

    var custList = from cust in xDoc.Descendants("Customer")
                             select new
                                {
                                    CustomerName = cust.Element("Name").Value,
                                    JobTitle = cust.Element("JobTitle").Value,
                                    Fax = cust.Element("Fax").Value,
                                    EmailId = cust.Element("EmailId").Value
                                };
    foreach (var cust in custList)
    {
        Console.WriteLine(cust.CustomerName+";"+cust.JobTitle+";"+cust.Fax+";"+
        cust.EmailId);
    }

   Output
(iii) Retrieving all the records where city=Bangalore
    
  var blrCustomer = from custList in xDoc.Descendants("Customer")
                                     where custList.Element("City").Value == "Bangalore"
                                     select new
                                     {
                                             CustomerName = custList.Element("Name").Value,
                                            JobTitle = custList.Element("JobTitle").Value,
                                            Fax = custList.Element("Fax").Value,
                                            EmailID = custList.Element("EmailId").Value
                                      };
    foreach (var cust in blrCustomer)
    {
         Console.WriteLine(cust.CustomerName + ";" + cust.JobTitle + ";" + cust.Fax + ";"
                                           cust.EmailID);
    }

    Output

(iv) Retrieving all the emailids from the xml document

    var custEmailIDList = from custList in xDoc.Descendants("Customer")
                                           select new {EmailID=custList.Element("EmailId").Value};

    foreach (var email in custEmailIDList)
    {
         Console.WriteLine(email.EmailID);
    }

   Output
(v) Counting the length of character in name and then retrieving the customer records        matching with the maximum value.
    
    var custLength = from custList in xDoc.Descendants("Customer")
                                   where custList.Element("Name").Value.Length== 
                                                               ( from n in xDoc.Descendants("Customer")
                                                                  orderby n.Element("Name").Value.Length descending
                                                                  select n.Element("Name").Value.Length).First()
                                  select new
                                             {
                                                     CustomerName = custList.Element("Name").Value,
                                                     JobTitle = custList.Element("JobTitle").Value,
                                                     Fax = custList.Element("Fax").Value,
                                                     EmailID = custList.Element("EmailId").Value
                                             };
   foreach (var cust in custLength)
   {
        Console.WriteLine(cust.CustomerName + ";" + cust.JobTitle + ";" + cust.Fax + ";"
                                         cust.EmailID);
   }

Output
As you can see from the output that only those names, of which length is matching with the name of maximum length is displayed in the output.

I hope these examples gives clear idea about LINQ to XML features.

Happy Coding.....