HOME ABOUT
I AM HERE
  • Twitter icon
  • Facebook icon
  • Technorati icon
Bookmark and Share

Simple Linq to XML example

November 30, 2011 14:26 by Aidan

 string xml = "<?xml version='1.0' encoding='utf-8'?><people>"
                + "<person><id>1</id><name>Aidan Garnish</name><company>65hours</company><email>aidan@65hours.com</email></person>"
                + "<person><id>2</id><name>Joe Bloggs</name><company>65hours</company><email>Fred@65hours.com</email></person>"
                + "<person><id>3</id><name>Fred Smith</name><company>Microsoft</company><email>Joe@Microsoft.com</email></person></people>";

           

            XDocument doc = XDocument.Parse(xml);

            //filter by company and return a collection of names as strings
            var query = from person in doc.Descendants("person")
              where (string)person.Element("company") == "65hours"
              select (string)person.Element("name");

            foreach (string name in query)
            {
                Console.WriteLine(name);
            }

 

            //return a collection of objects
            var query2 = from person in doc.Descendants("person")
                    where (string)person.Element("company") == "65hours"
                    select new
                         {
                             id = person.Element("id").Value,
                             name = person.Element("name").Value,
                             email =person.Element("email").Value,
                             company = person.Element("name").Value
                         };
           
            foreach (var obj in query2)
            {
                Console.WriteLine(obj.id + " - " + obj.name);
            }           

            Console.ReadLine();


Tags:
Categories: Linq
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Some simple Linq to Objects examples

November 30, 2011 13:57 by Aidan

Linq to Objects is a really handy way to filter and order lists of objects. The following code is a simple example that shows how to filter and how to order a list by an object property.

//a little bit of object and list set up
            ContractPerson person1 = new ContractPerson()
            {   Name = "Aidan Garnish",
                AccountID = "1",
                Company = "65hours",
                Email = "aidan@65hours.com",
                Role = "SharePoint Consultant" };

            ContractPerson person2 = new ContractPerson()
            {
                Name = "Fred Smith",
                AccountID = "2",
                Company = "65hours",
                Email = "fred@65hours.com",
                Role = "Dynamics Consultant"
            };

            ContractPerson person3 = new ContractPerson()
            {
                Name = "Joe Blogs",
                AccountID = "3",
                Company = "Microsoft",
                Email = "joe@ms.com",
                Role = "Microsoft Consultant"
            };

            List<ContractPerson> people = new List<ContractPerson>();
            people.Add(person1);
            people.Add(person2);
            people.Add(person3);


            //filter list by ContractPerson.Company
            var filteredPeople = from person in people
                        where person.Company == "65hours"
                        select person;
           
            foreach (ContractPerson filterPerson in filteredPeople)
            {
                Console.WriteLine(filterPerson.Name);
            }

            //filter list by people who have a name beginning with A
            var peopleWithNameStartingA = from person in people
                                 where person.Name.StartsWith("A")
                                 select person;

            foreach (ContractPerson filterPerson in peopleWithNameStartingA)
            {
                Console.WriteLine(filterPerson.Name);
            }

            //order the list
            var orderedList = from person in people
                              orderby person.Name ascending
                              select person;

            foreach (ContractPerson orderedPerson in orderedList)
            {
                Console.WriteLine(orderedPerson.Name);
            }

            Console.ReadLine();


Tags:
Categories: Linq
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed