Getting the string values of SPMetal generated Choice fields

Posted: 2nd June 2011 in LINQ 2 Sharepoint, Sharepoint 2010

When you use SPMetal.exe to generate strongly typed entity classes to represent your SharePoint site, all lists which contain choice fields are created as enumerators. However, the references to the enumerator created in the class are pointing to a System.Nullable of the enum.

This is not a big deal to convert, but it does cause a bit of a problem when using anonymous types from LINQ 2 SharePoint.  As I’m sure you are aware, any assignment within the select new of the anonymous type has to be either; assigned directly or by the return value of a method – you cannot manipulate the string within the select.

So, to ease my pain, I created a generic method, which i have added to my SPHelper static class, to provide this functionality so I didn’t have to write a different method for every enumerator type and I thought I would just share it.

Get string from nullable enum
  1.   /// <summary>
  2.         /// Gets the nullable enumerator string.
  3.         /// </summary>
  4.         /// <typeparam name="T">A Nullable enumerator</typeparam>
  5.         /// <param name="enumerator">The enumerator.</param>
  6.         /// <param name="nullValue">The value to be returned if the enumerator doesnt have a value.</param>
  7.         /// <returns>A string representation of the enum value </returns>
  8.         public static string GetNullableEnumeratorString<T>(T? enumerator, string nullValue) where T : struct, IConvertible
  9.         {
  10.             if (!typeof(T).IsEnum)
  11.             {
  12.                 throw new ArgumentException("T must be an enumerated type");
  13.             }
  14.  
  15.             return enumerator.HasValue ? enumerator.Value.ToString() : nullValue;
  16.         }

Seen below is and example of how to call this method from the LINQ select statement.

Assigmment by Method – Anon T
  1. var employees = from si in sensitiveInformation.ToList().Distinct()
  2.                                where si.Employee.Created >= lastRunTime
  3.                                select
  4.                                    new
  5.                                    {
  6.                                        FirstName = si.Employee.FullName,
  7.                                        LastName = si.Employee.LastName,
  8.                                        Gender =
  9.                                            SPHelper.GetNullableEnumeratorString(si.Employee.Gender, "Unassigned"),
  10.                                    };

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s