rulururu

post Null coalescing operator and LINQ

January 24th, 2008

Filed under: .NET — Kai @ 11:40 pm

I recently wrote about the coalescing operator and its usage, now I’d like to show you a really effective way to use in with LINQ.

First Step:

What’s LINQ (Language Integrated Query)?
LINQ was released as a part of .NET Framework 3.5. It defines a set of query operators that can be used to query, project and filter data in arrays, enumerable classes, XML, relational database, and third party data sources. While it allows any data source to be queried, it requires that the data be encapsulated as objects.
The syntax is mostly like SQL and IntelliSense helps you greatly with it ;-)

For some introduction I can advice two articles by Thiru Thangarathinam on 15Seconds:

Seconds Step:

I create a sample XML file filled with some test data:

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> 
 <contacts> 
      <member> 
           <firstname>Tom</firstname>
           <lastname>Morello</lastname>
      </member>
<member> 
           <firstname>Jon</firstname> 
           <lastname>Bon Jovi</lastname> 
      </member> 
 </contacts>

When handling raw XML are cases where XML shapes are irregular and/or missing elements/attributes.

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> 
 <contacts> 
      <member> 
           <firstname>Tom</firstname>
           <lastname>Morello</lastname>
      </member>
<member> 
           <firstname>Jon</firstname> 
           <lastname>Bon Jovi</lastname> 
<age>46</age> 
      </member> 
 </contacts>

We got a tag that is not always present for every contact in the file. The ?? operator can be very useful in these scenarios.

Third Step - Solution:

Reading the XML data with LINQ is quite simple and can be done with something along those lines:

XDocument contFile = XDocument.Load(File.FromFile("my.xml"));
var contacts = from c in contFile.Descendants("contact")
	select new {
		_sName = (string) c.Element("firstname"),
		_sFName = (string) c.Element("lastname"),
		_iAge = (int) c.Element("age")
	};

This will yield poorly when not finding the element “age” in the first block.
I’m using an explecit conversation for writing the tags into the vars by casting them e.g. to int. One way to fix this is to modify our LINQ to XML query so that we indicate that _iAge is a nullable integer.

Cast to be (int?) instead of (int):

_iAge = (int?) c.Element("age");

But what can we do if we don’t want an empty value for missing tags in the XML? We can use the coalescing operator to set a default value for every missing element.

_iAge = (int?) c.Element("age")?? 0;

Now instead of a blank output the defaultvalue 0 is shown.

Conclusion:

You can use the .NET ?? operator in many types of scenarios. Not just to prevent yourself from annoying excessive code writing also for better maintainability of the source and clear programming.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)