Aidan Garnish

Collaboration Not Competition

Simple Linq to XML example

 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();

Some simple Linq to Objects examples

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();

Book#Hashtags and PayPal integration

Quite a few people have been asking how they can support http://bookhashtags.com or have their book featured on the home page.

As a result I have combined these two things together and it is now possible for authors or publishers to feature their books on the home page by visiting http://www.bookhashtags.com/getyourbookfeatured

This was achieved using PayPal's Express Checkout API to take payment - I am thinking of writing a blog post on using PayPal Express Checkout with C# ASP.Net so if that would be useful to you please let me know in the comments. Although PayPal has a lot of documentation finding the relevant parts and putting them all together did take quite a bit of time and effort so maybe a blog post on this could save other people some pain?