/// <summary> /// Reads a single "group by" field value from the data reader, coerces it /// to the correct type if necessary/possible, and returns it. /// </summary> /// <param name="mapping">Mapping of class fields / names / etc.</param> /// <param name="reader">Data reader to get the value from.</param> /// <param name="number">Which group by field is this (0th, 1st, etc).</param> /// <param name="expression">The group by expression we're reading the value for.</param> /// <returns>The value to put in the GroupValues collection of the GroupCountResult.</returns> protected virtual object GetGroupByValue(ClassMapping mapping, IDataReader reader, int number, AbstractGroupExpression expression) { // We aliased the group bys in order as "gb_0", "gb_1", etc. string colName = "gb_" + number; int colNum = reader.GetOrdinal(colName); object value = reader.IsDBNull(colNum) ? null : reader.GetValue(colNum); if (expression is MemberGroupExpression) { // For group bys, we leave nulls as nulls even if the type would // normally be non-nullable (I.E. int, float, etc). if (value != null) { string memberName = ((MemberGroupExpression) expression).MemberName; Type desiredType = null; // If we actually have a member info, coerce the value to that type. if (mapping.AllObjMemberInfosByObjAttr.ContainsKey(memberName)) { MemberInfo info = mapping.AllObjMemberInfosByObjAttr[memberName]; // Don't call MemberType getter twice MemberTypes type = info.MemberType; if (type == MemberTypes.Field) { FieldInfo fInfo = ((FieldInfo) info); desiredType = fInfo.FieldType; } else if (type == MemberTypes.Property) { PropertyInfo pInfo = ((PropertyInfo) info); desiredType = pInfo.PropertyType; } } else if (mapping.DataColTypesByObjAttr.ContainsKey(memberName)) { // We don't have a memberinfo, but we do have a type in the mapping, // so coerce to that type. desiredType = mapping.DataColTypesByObjAttr[memberName]; } // If we have a type to coerce it to, coerce it, otherwise return as-is. if (desiredType != null) { value = CoerceType(desiredType, value); } } } else { throw new ArgumentException( "Group expression '" + expression + "' is an unsupported type.", "expression"); } return value; }