Category Archives: Linq

Reading an XML file using Linq to XML

A quick and post and more for my own reference but I recently needed to read some elements from an XML document which can be accomplished much easier these days using Linq to XML.

So if I have a simple XML document :

Sample XML document

I can read this using Linq as below (language is C#) :

C# routine to parse the XML document using Linq

In this method I am first loading the XML file into an XDocument Object found under the System.XML.Linq namespace.

I am then using Linq to return a collection of Items which represent the Parent ‘Client’ Nodes, using the Descendants function.

Then we can loop though each Client Node and simply extract the Element Values for the KEY and POSITION Nodes.  Its also worth noticing in this example that I have included an attribute at the CLIENT Node level.  Using Attributes().First() I am requesting the first attribute found attached to my CLIENT object.

Like I mentioned nothing difficult here but a handy little reference

Leave a comment

Posted by on June 22, 2012 in C#, Linq, Uncategorized, XML


Update a record through the Entity Framework using Attach

Performing basic Create, Read, Update and Delete Operations via the Entity Framework is very straight forward.  However take this scenario :

  1. The EF is used to return a Customer Record to the Presentation Layer
  2. A User then modifies the Customer record
  3. The modified record is sent back to the EF to update the record in the database.

To Update the record we can write the following code :

public bool UpdateCustomer(Customer customer){

    Customer cust = entities.Customers.FirstOrDefault(c => c.ID == customer.ID);

    cust.Forname = customer.Forename;

    cust.Surname = customer.Surname;



But what if the object we are updating contains a lot of attributes.  Surely updating each attribute seperately before calling a Save is a little overkill.

Luckily we are providied with a solution which allows us to attach to a record within the context, perform a straight replace on the record with the new modified record and save back the changes, and all in just 3 lines of code:

bool UpdateCustomer(Customer customer) {

    entities.Customers.Attach(entities.Customers.Single(c => c.ID == customer.ID));





C# Tips, Tricks and Handy Hints

Part of the reason I started a Development blog in the first place was to create a common place to record all things dev that I didn’t want to forget.  Lots of these points fall into a ‘handy to know little tip that I might use more often than not but not worth writing a whole blog about’ sort of category!!  So I’ve created a dedicated blog post just for collecting all these small C# tips and tricks together in one place starting with Ordering Custom Collections.

Feel free to pass on your own tips and tricks and I’ll add them to the Post

TIP 1 : Ordering a Generic List Collection using Lambda expressions

Assume that I have a collection of List<Customer> Objects called Customers and I want to sort by Customer.Surname in alphabetical order

The easiest way is with this simple one line of code:

return Customers.OrderBy(x => x.Surname).ToList();

TIP 2 : Using Linq to Split a string and convert to an array of a different type using Lambda expressions

We all know you can use the Split function to split a string by a specified character and populate a string array.  However what if you want to populate an array of integers for example.  You might think we have to loop through each element in the string array to cast the element as an integer and populate into a new integer array.  Well with Linq you can do all this with one line of code:

string _numbers = “2126,2128,234,2”;

Int32[] iNumbers = Array.ConvertAll(_numbers.Split(‘,’), x => Convert.ToInt32(x));

TIP 3 : Distinct on a Linq to Entities Statement

Sticking with the Linq theme, we all know about performing a ‘SELECT DISTINCT ****’ style statement in SQL but how can we perform a similar Distinct when writing a Linq statement. Heres how:

(from cust in entities.Customers select cust).Distinct();

Simple but handy to know

TIP 4 : Getting the Assembly Version of the Current Project

I was working on a windows form project the other day and had a requirement to display the Version of the Assembly on the form.  To grab the version from the AssemblyInfo realting to the current project you can do the following:

string appName = System.Reflection.Assembly.GetAssembly(this.GetType()).Location;

System.Reflection.AssemblyName assemblyName = System.Reflection.AssemblyName.GetAssemblyName(appName);

string version = assemblyName.Version.ToString();

TIP 5 : Inserting a Quote into a string

Very quick this one, but one of those things that always gets passed over the table ‘How do you do that again?’

Simple preceed the Quote with the escape character ‘\’

For Example to output “This is a string surrounded by quotes” use

string sentence = “\”This is a string surrounded by quotes\””;

Leave a comment

Posted by on April 20, 2011 in ASP.NET, C#, Linq