Ejemplo n.º 1
0
        internal static TypeCode TypeCodeFromType(Type type)
        {
            if (s_typeToTypeCodeMap == null)
            {
                lock (s_lock) {
                    if (s_typeToTypeCodeMap == null)
                    {
                        //
                        Dictionary <Type, TypeCode> typeNameToTypeCode = new Dictionary <Type, TypeCode>();
                        typeNameToTypeCode[typeof(Boolean)]  = TypeCode.Boolean;
                        typeNameToTypeCode[typeof(String)]   = TypeCode.String;
                        typeNameToTypeCode[typeof(Byte)]     = TypeCode.Byte;
                        typeNameToTypeCode[typeof(Int16)]    = TypeCode.Int16;
                        typeNameToTypeCode[typeof(Int32)]    = TypeCode.Int32;
                        typeNameToTypeCode[typeof(Int64)]    = TypeCode.Int64;
                        typeNameToTypeCode[typeof(Single)]   = TypeCode.Single;
                        typeNameToTypeCode[typeof(Double)]   = TypeCode.Double;
                        typeNameToTypeCode[typeof(Decimal)]  = TypeCode.Decimal;
                        typeNameToTypeCode[typeof(DateTime)] = TypeCode.DateTime;
                        typeNameToTypeCode[typeof(Char)]     = TypeCode.Char;

                        // We don't support columns of type 'sqlvariant', which show up as Object
                        //
                        typeNameToTypeCode[typeof(Object)] = TypeCode.DBNull;

                        // We don't support byte arrays.  This include columns of type 'timestamp'
                        typeNameToTypeCode[typeof(Byte[])] = TypeCode.DBNull;

                        // Use Object for Guid's (though we need to do some special processing)
                        typeNameToTypeCode[typeof(Guid)] = TypeCode.Object;

                        s_typeToTypeCodeMap = typeNameToTypeCode;
                    }
                }
            }

            // If it's an Nullable<T>, work with T instead
            type = Misc.RemoveNullableFromType(type);

            TypeCode typeCode;

            if (s_typeToTypeCodeMap.TryGetValue(type, out typeCode))
            {
                return(typeCode);
            }

            return(TypeCode.Object);
        }
        public static IQueryable ApplyEqualityFilter(IQueryable source, string propertyName, object value)
        {
            ParameterExpression parameterExpression = Expression.Parameter(source.ElementType, String.Empty);
            Expression          propertyExpression  = CreatePropertyExpression(parameterExpression, propertyName);

            if (Nullable.GetUnderlyingType(propertyExpression.Type) != null && value != null)
            {
                propertyExpression = Expression.Convert(propertyExpression, Misc.RemoveNullableFromType(propertyExpression.Type));
            }
            value = Misc.ChangeType(value, propertyExpression.Type);
            Expression           compareExpression = Expression.Equal(propertyExpression, Expression.Constant(value));
            LambdaExpression     lambda            = Expression.Lambda(compareExpression, parameterExpression);
            MethodCallExpression whereCall         = Expression.Call(typeof(Queryable), "Where", new Type[] { source.ElementType }, source.Expression, Expression.Quote(lambda));

            return(source.Provider.CreateQuery(whereCall));
        }