예제 #1
0
        virtual public CommonResponse GetSingleWhere(string prop, string q_value)
        {
            CommonResponse response = new CommonResponse();
            List <Expression <Func <Entity, bool> > > wheres = new List <Expression <Func <Entity, bool> > >();

            try
            {
                string sPropertyName = prop;//.Substring(6);

                PropertyInfo oProp = typeof(Entity).GetProperty(sPropertyName);
                Type         tProp = oProp.PropertyType;
                //Nullable properties have to be treated differently, since we
                //  use their underlying property to set the value in the object
                if (tProp.IsGenericType &&
                    tProp.GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
                {
                    //Get the underlying type property instead of the nullable generic
                    tProp = new NullableConverter(oProp.PropertyType).UnderlyingType;
                }

                ParameterExpression entityParameter = Expression.Parameter(typeof(Entity), "entityParameter");
                Expression          childProperty   = Expression.PropertyOrField(entityParameter, sPropertyName);


                var value = Expression.Constant(Convert.ChangeType(q_value, tProp));

                // let's perform the conversion only if we really need it
                var converted = value.Type != childProperty.Type
                    ? Expression.Convert(value, childProperty.Type)
                    : (Expression)value;

                Expression comparison = Expression.Equal(childProperty, converted);

                Expression <Func <Entity, bool> > lambda = Expression.Lambda <Func <Entity, bool> >(comparison, entityParameter);

                wheres.Add(lambda);
            }
            catch (Exception ex)
            {
                return(response.Error(ex.ToString()));
            }
            return(_logic.GetSingleWhere(wheres.ToArray()));
        }
        virtual public CommonResponse GetSingleWhere(string prop, string value)
        {
            CommonResponse response = new CommonResponse();
            List <Expression <Func <Entity, bool> > > wheres = new List <Expression <Func <Entity, bool> > >();

            try
            {
                ParameterExpression entityParameter = Expression.Parameter(typeof(Entity), "entityParameter");
                Expression          childProperty   = Expression.PropertyOrField(entityParameter, prop);

                Expression comparison = Expression.Equal(childProperty, Expression.Constant(value));

                Expression <Func <Entity, bool> > lambda = Expression.Lambda <Func <Entity, bool> >(comparison, entityParameter);

                wheres.Add(lambda);
            }
            catch (Exception ex)
            {
                return(response.Error(ex.ToString()));
            }
            return(_logic.GetSingleWhere(wheres.ToArray()));
        }