nullable average in linq

posted by alexis reigel on october 12, 2008

The following code is one possibility (there may be more elegant solutions to this) to handle a nullable average from a linq query. The problem arises when the linq query does not return a result, thus the average being null. If you don’t use a nullable type, you’ll get an exception. To get a nullable average from an integer value you’ll need to provide a transform function to the Average funtion.

double? nullableAverage = 	
    (from m in dataContext.Moods 	 
	join g in dataContext.Groups on m.GroupGUID equals g.GUID 	 
	where g.GUID == groupGUID 	 
	select m.Level) 	
	.Average<int>(x => new double?(x));  
int average = (int)Math.Round(nullableAverage.HasValue ? nullableAverage.Value : 0, 0);  
// ... 

custom application settings

posted by alexis reigel on june 28, 2008

Within a c# winform application I needed to store a generic collection to my settings, which I found out was not possible out of the box. It is possible though to set the required type in the properties designer dialog, but the serialization’s output is always nothing.

On my research on the internet for a solution i stumbled upon the post Tips for C# .NET Software Developers, which described that the attribute [SettingsSerializeAs(SettingsSerializeAs.Binary)] was required for the settings property, and to set it accordingly in the Settings.Designer.cs file. The problem here is that modifying designer generated code file is not the best of all ideas, as the manual changes are overwritten every time the settings are modified by the designer. Thus, I followed the msn article on how to create application settings, which told me to create a custom settings class. The drawback with this solution is that I now have two settings classes, and not only one single to cover it all.

Here’s an example of the settings class. Note the use of the SettingsSerializeAs attribute, as it is the key to the successful serialization.

using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;

namespace Koffeinfrei
{
    /// <summary>
    ///   This class provides extended settings. Use this class for types that need to have special     
    ///   attributes for serialization, that cannot be set using the config designer dialog.
    /// </summary>
    internal class ExtendedSettings : ApplicationSettingsBase
    {
        private static readonly ExtendedSettings defaultInstance =
            ((ExtendedSettings) (Synchronized(new ExtendedSettings())));

        /// <summary>
        ///   Returns the default instance of this class.
        /// </summary>
        /// <remarks>
        ///   Get the value of the <see name="defaultInstance" /> member.
        /// </remarks>
        public static ExtendedSettings Default
        {
            get { return defaultInstance; }
        }

        /// <summary>
        ///   The <see cref="SomeCustomClass" />s stored in the user scoped settings.
        /// </summary>
        /// <remarks>
        ///   Get / set the value of the <c>CustomClasses</c> property.
        /// </remarks>
        [UserScopedSetting]
        [DebuggerNonUserCode]
        [SettingsSerializeAs(SettingsSerializeAs.Binary)]
        public Queue<SomeCustomClass> CustomClasses
        {
            get { return ((Queue<SomeCustomClasses>) (this["CustomClasses"])); }
            set { this["CustomClasses"] = value; }
        }
    }
}

EDIT

Thanks to partial classes you can merge the custom settings to the main settings. Just use the same class name internal class Settings.