示例#1
0
        private string DynamicWhere2(List <QueryParameter> queryProperties, Type currentType)
        {
            var builder = new StringBuilder();

            builder.Append(" Where ");

            for (int i = 0; i < queryProperties.Count(); i++)
            {
                QueryParameter item = queryProperties[i];

                var fieldType = GePropertyInfo(currentType, item.PropertyName); // currenttype.GetField(item.PropertyName, BindingFlags.PutRefDispProperty  );

                var columnTable = string.Empty;
                var param       = string.Empty;

                if (fieldType != null)
                {
                    columnTable = fieldType.GetCustomAttribute <ColumnAttribute>().Name;
                    param       = columnTable;
                }

                var dialeto = DapperExt.GetDialect();

                var prefixParam = dialeto == "Oracle" ? ":" : "@";

                if (!string.IsNullOrEmpty(columnTable))
                {
                    if (!string.IsNullOrEmpty(item.LinkingOperator) && i > 0)
                    {
                        //builder.Append(string.Format("{0} {1} {2} @{1} ", item.LinkingOperator, item.PropertyName, item.QueryOperator));
                        builder.Append(string.Format("{0} {1} {2} {3} ", item.LinkingOperator, DapperExt.Encapsulate(columnTable), item.QueryOperator, item.PropertyValue));
                    }
                    else
                    {
                        //builder.Append(string.Format("{0} {1} @{0} ", item.PropertyName, item.QueryOperator));

                        builder.Append(string.Format("{0} {1} {2} ", DapperExt.Encapsulate(columnTable), item.QueryOperator, item.PropertyValue));
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(item.LinkingOperator) && i > 0)
                    {
                        builder.Append(string.Format("{0} {1} {2} {3} ", item.LinkingOperator, item.PropertyName, item.QueryOperator, item.PropertyValue));
                    }
                    else
                    {
                        builder.Append(string.Format("{0} {1} {2} ", item.PropertyName, item.QueryOperator, item.PropertyValue));
                    }
                }
            }

            return(builder.ToString());
        }
示例#2
0
        private QueryResult DynamicWhere(List <QueryParameter> queryProperties, Type currentType)
        {
            IDictionary <string, Object> expando = new ExpandoObject();
            QueryResult result  = null;
            var         builder = new StringBuilder();

            builder.Append(" where ");

            for (int i = 0; i < queryProperties.Count(); i++)
            {
                QueryParameter item = queryProperties[i];

                var fieldType = GePropertyInfo(currentType, item.PropertyName); // currenttype.GetField(item.PropertyName, BindingFlags.PutRefDispProperty  );

                var columnTable = string.Empty;
                var param       = string.Empty;

                if (fieldType != null && fieldType.GetCustomAttribute <ColumnAttribute>() != null)
                {
                    columnTable = fieldType.GetCustomAttribute <ColumnAttribute>().Name;
                    param       = columnTable;
                }
                else if (fieldType != null && fieldType.CustomAttributes.Count() > 0)
                {
                    //CustomAttributeData customAttrib = fieldType.GetCustomAttributesData().First(x => x.AttributeType == typeof(ColumnAttribute));

                    //if (customAttrib != null )
                    //{
                    //    columnTable = ((ColumnAttribute)customAttrib).Name;
                    //    param = columnTable;
                    //}

                    foreach (var atrib in fieldType.GetCustomAttributesData())
                    {
                        if (atrib.AttributeType == typeof(System.ComponentModel.DataAnnotations.Schema.ColumnAttribute) || atrib.AttributeType == typeof(Dapper.DapperExt.ColumnAttribute))
                        {
                            string attributeTypeName = atrib.AttributeType.Name;

                            columnTable = atrib.ConstructorArguments[0].Value?.ToString();
                            param       = columnTable;
                        }
                        //else if (atrib.AttributeType == typeof(Dapper.DapperExt.ColumnAttribute))
                        //{
                        //    columnTable = (Dapper.DapperExt.ColumnAttribute)atrib).Name;
                        //    param = columnTable;
                        //}
                    }
                }

                var dialeto = DapperExt.GetDialect();

                var prefixParam = dialeto == "Oracle" ? ":" : "@";

                if (!string.IsNullOrEmpty(columnTable))
                {
                    if (!string.IsNullOrEmpty(item.LinkingOperator) && i > 0)
                    {
                        //builder.Append(string.Format("{0} {1} {2} @{1} ", item.LinkingOperator, item.PropertyName, item.QueryOperator));
                        builder.Append(string.Format("{0} {1} {2} {3} ", item.LinkingOperator, DapperExt.Encapsulate(columnTable), item.QueryOperator, prefixParam + param));
                    }
                    else
                    {
                        //builder.Append(string.Format("{0} {1} @{0} ", item.PropertyName, item.QueryOperator));

                        builder.Append(string.Format("{0} {1} {2} ", DapperExt.Encapsulate(columnTable), item.QueryOperator, prefixParam + param));
                    }

                    expando[columnTable] = item.PropertyValue;
                }
                else
                {
                    if (!string.IsNullOrEmpty(item.LinkingOperator) && i > 0)
                    {
                        builder.Append(string.Format("{0} {1} {2} {3} ", item.LinkingOperator, item.PropertyName, item.QueryOperator, prefixParam + item.PropertyName));
                    }
                    else
                    {
                        builder.Append(string.Format("{0} {1} {2} ", item.PropertyName, item.QueryOperator, prefixParam + item.PropertyName));
                    }

                    expando[item.PropertyName] = item.PropertyValue;
                }
            }

            result = new QueryResult(builder.ToString().TrimEnd(), expando);

            return(result);
        }