Ejemplo n.º 1
0
 /// <summary>
 /// Update where condition. The DefaultValue attribute is used as a filter.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="target"></param>
 /// <param name="exact">Is compared value exact or containing given value.</param>
 public void FilterBy <T>(T target, bool exact)
 {
     if (target != null)
     {
         Dictionary <string, GXSerializedItem> properties = GXSqlBuilder.GetProperties(GXInternal.GetPropertyType(target.GetType()));
         foreach (var it in properties)
         {
             if ((it.Value.Attributes & Attributes.DefaultValue) != 0 && it.Value.Get != null)
             {
                 object actual = it.Value.Get(target);
                 if (actual != null && it.Value.DefaultValue == null)
                 {
                     if (actual is DateTime d)
                     {
                         if (d == DateTime.MinValue)
                         {
                             continue;
                         }
                     }
                     else if (actual is DateTimeOffset dto)
                     {
                         if (dto.DateTime == DateTime.MinValue)
                         {
                             continue;
                         }
                     }
                     else if (actual is Guid q)
                     {
                         if (q == Guid.Empty)
                         {
                             continue;
                         }
                     }
                     else if (!(actual is string) &&
                              typeof(System.Collections.IEnumerable).IsAssignableFrom(actual.GetType()))
                     {
                         //Arrays and lists are not filtered.
                         continue;
                     }
                     else if (!(actual is string) && actual.GetType().IsClass)
                     {
                         FilterBy(actual, exact);
                         continue;
                     }
                 }
                 if (Convert.ToString(it.Value.DefaultValue) != Convert.ToString(actual))
                 {
                     if (actual != null)
                     {
                         if (actual.GetType().IsEnum)
                         {
                             actual = Convert.ToInt64(actual);
                         }
                         if (actual is bool b)
                         {
                             int val = b ? 1 : 0;
                             And <T>(q => it.Value.Target.Equals(val));
                         }
                         else if (exact || actual is Guid)
                         {
                             And <T>(q => it.Value.Target == actual);
                         }
                         else
                         {
                             if (actual is DateTime d)
                             {
                                 if (d == DateTime.MinValue || d == DateTime.MaxValue)
                                 {
                                     And <T>(q => it.Value.Target == actual);
                                 }
                                 else
                                 {
                                     And <T>(q => (DateTime)it.Value.Target >= d);
                                 }
                             }
                             else
                             {
                                 And <T>(q => GXSql.Contains(it.Value.Target, actual));
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Update where condition. The DefaultValue attribute is used as a filter.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="target"></param>
        /// <param name="exact">Is compared value exact or containing given value.</param>
        public void FilterBy <T>(T target, bool exact)
        {
            if (target != null)
            {
                Dictionary <string, GXSerializedItem> properties = GXSqlBuilder.GetProperties(GXInternal.GetPropertyType(target.GetType()));
                foreach (var it in properties)
                {
                    if ((it.Value.Attributes & Attributes.DefaultValue) != 0)
                    {
                        object actual = it.Value.Get(target);
                        if (it.Value.DefaultValue == null)
                        {
                            if (actual is DateTime d)
                            {
                                if (d == DateTime.MinValue)
                                {
                                    continue;
                                }
                            }
                            if (actual is Guid q)
                            {
                                if (q == Guid.Empty)
                                {
                                    continue;
                                }
                            }
                        }
                        if (Convert.ToString(it.Value.DefaultValue) != Convert.ToString(actual))
                        {
                            if (actual != null)
                            {
                                if (actual.GetType().IsEnum)
                                {
                                    actual = Convert.ToInt64(actual);
                                }

                                if (exact || actual is Guid)
                                {
                                    And <T>(q => it.Value.Target == actual);
                                }
                                else
                                {
                                    if (actual is DateTime d)
                                    {
                                        if (d == DateTime.MinValue || d == DateTime.MaxValue)
                                        {
                                            And <T>(q => it.Value.Target == actual);
                                        }
                                        else
                                        {
                                            And <T>(q => (DateTime)it.Value.Target >= d);
                                        }
                                    }
                                    else
                                    {
                                        And <T>(q => GXSql.Contains(it.Value.Target, actual));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }