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;
}