The C# ?? null coalescing operator (and using it with LINQ)
page 3 of 5
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 24027/ 57

Using the ?? operator with LINQ

Last month I wrote a blog post that covered using the new LINQ to XML support in .NET 3.5.  One of the "gotchas" you often need to deal with when handling raw XML are cases where XML shapes are irregular and/or missing elements/attributes.  The ?? operator can be very useful in these scenarios.

For example, let's consider a scenario where we have an XML file or feed with the following contact data:

Figure 5

We could write the below LINQ to XML code in C# to open the XML file and retrieve back a sequence of anonymous type objects with "Name", "Title", "Email" and "YearsAtCompany" properties, and then databind the results to an <asp:gridview> control on a page:

Figure 6

Notice above how I'm using the explicit conversion operator support on the XElement class to retrieve strongly typed values from the XML.  This enables me to just cast the c.Element("YearsAtCompany") value to an int - and it will then automatically convert the string value to an integer for me, and type the "YearsAtCompany" property on my anonymous type to be an int.

When we run the above code snippet, we'll then get a nice Gridview listing of our contacts:

Figure 7

This explicit conversion support works great when the <YearsAtCompany> element is always defined.  But it will throw a runtime error in the case where we have a <Contact> that is missing a <YearsAtCompany> sub-element:

Figure 8

 

One way to fix this is to modify our LINQ to XML query so that we indicate that YearsAtCompany is a nullable integer.  We can do this by changing the explicit cast to be (int?) instead of (int):

Figure 9

This enables our query to execute cleanly and not raise any errors.  Instead a null value will be assigned to the YearsAtCompany property if no <YearsAtCompany> element is present in the XML. 

When we run the application and databind the results to the Gridview, you can see the YearsAtCompany column for our third contact is empty as a result (since the value is null):

Figure 10

But what if we didn't want a missing XML value to result in a null integer value - but instead just indicate a value of 0? 

Well.... that is where we can use the new ?? operator support.  We can just modify the LINQ to XML query like below to indicate a default value of 0 if no element is present:

Figure 11

This indicates that if the <YearsAtCompany> element is missing in the XML, and the result would otherwise be null, instead assign a value of 0.  Notice in the intellisense above how C# automatically detects that this means that the YearsAtCompany property on the new anonymous type will never be null - and so it marks the property to be of type (int) instead of (int?). 

And now when we run the page we'll see a value of 0 show up in our third row instead of a blank value:

Figure 12


View Entire Article

User Comments

Title: C # Linq   
Name: Faheem
Date: 5/21/2009 7:43:20 AM
Comment:
hi
say i have 300 xml elements and 250 may or may not have their values in xml file then it will be very tedious for me to write all element name and using ? style as u mention . IS there any method which is universal and easy to use.
Title: ASP .Net   
Name: raja
Date: 10/5/2007 3:36:50 AM
Comment:
detail Above session






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 11/21/2014 8:31:31 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search