internal static DataTablePropertyInfoModel[] GetProperties(Type type) { return(PropertiesCache.GetOrAdd(type, t => { var listDataTablePropertyInfo = new List <DataTablePropertyInfoModel>(); var listPropertyInfo = t.GetProperties(); foreach (var propertyInfo in listPropertyInfo) { if (propertyInfo.GetCustomAttribute <DataTableIgnoreAttribute>() != null) { // Ignore Property have DataTableIgnoreAttribute continue; } var attributes = propertyInfo.GetCustomAttributes <DataTableBaseAttribute>().ToArray(); // Add to List Property var dataTablePropertyInfo = new DataTablePropertyInfoModel(propertyInfo, attributes) { Order = attributes.OfType <DataTableAttribute>().Select(a => a.Order).SingleOrDefault() }; listDataTablePropertyInfo.Add(dataTablePropertyInfo); } // Order listDataTablePropertyInfo = listDataTablePropertyInfo.OrderBy(x => x.Order).ToList(); return listDataTablePropertyInfo.ToArray(); })); }
private static bool IsDateTimeOffsetType(DataTablePropertyInfoModel propertyInfo) { var isDateTimeOffsetType = propertyInfo.Type == typeof(DateTimeOffset) || propertyInfo.Type == typeof(DateTimeOffset?); return(isDateTimeOffsetType); }
public static string BoolFilter(string terms, string columnName, DataTablePropertyInfoModel propertyInfo, List <object> parametersForLinqQuery) { if (string.IsNullOrWhiteSpace(terms)) { return(null); } var termsNorm = terms.Trim().ToLowerInvariant(); termsNorm = termsNorm.TrimStart('^').TrimEnd('$'); var termsLowerCase = termsNorm?.ToLowerInvariant(); if (termsLowerCase == DataConstants.FalseLower || termsLowerCase == DataConstants.TrueLower) { return(termsNorm.ToLower() == DataConstants.TrueLower ? $"{columnName} == {DataConstants.TrueLower}" : $"{columnName} == {DataConstants.FalseLower}"); } if (propertyInfo.Type != typeof(bool?)) { return(null); } return(DataConstants.Null.Equals(termsNorm, StringComparison.CurrentCultureIgnoreCase) ? $"{columnName} == {DataConstants.Null}" : null); }
public static string NumericFilter(string terms, string columnName, DataTablePropertyInfoModel propertyInfo, List <object> parametersForLinqQuery) { if (terms.StartsWith("^")) { terms = terms.TrimStart('^'); } if (terms.EndsWith("$")) { terms = terms.TrimEnd('$'); } if (terms == "~") { return(string.Empty); } string clause = null; if (terms.Contains("~")) { var parts = terms.Split('~'); try { parametersForLinqQuery.Add(ChangeType(parts[0], propertyInfo)); clause = $"{columnName} >= @{parametersForLinqQuery.Count - 1}"; } catch (FormatException) { } try { parametersForLinqQuery.Add(ChangeType(parts[1], propertyInfo)); if (clause != null) { clause += " and "; } clause += $"{columnName} <= @{parametersForLinqQuery.Count - 1}"; } catch (FormatException) { } return(clause ?? "true"); } try { parametersForLinqQuery.Add(ChangeType(terms, propertyInfo)); return($"{columnName} == @{parametersForLinqQuery.Count - 1}"); } catch (FormatException) { } return(null); }
internal static object ChangeType(string terms, DataTablePropertyInfoModel propertyInfo) { if (propertyInfo.PropertyInfo.PropertyType.GetTypeInfo().IsGenericType&& propertyInfo.Type.GetGenericTypeDefinition() == typeof(Nullable <>)) { var u = Nullable.GetUnderlyingType(propertyInfo.Type); return(Convert.ChangeType(terms, u)); } return(Convert.ChangeType(terms, propertyInfo.Type)); }
public static string StringFilter(string terms, string columnName, DataTablePropertyInfoModel propertyInfo, List <object> parametersForLinqQuery) { if (string.IsNullOrWhiteSpace(terms)) { return(null); } var termsNorm = terms.Trim().ToLowerInvariant(); if (termsNorm == ".*") { return(string.Empty); } string parameterArg; string clause; if (termsNorm.StartsWith("^")) { if (termsNorm.EndsWith("$")) { parametersForLinqQuery.Add(termsNorm.Substring(1, termsNorm.Length - 2)); parameterArg = $"@{parametersForLinqQuery.Count - 1}"; return($"{columnName} == {parameterArg}"); } parametersForLinqQuery.Add(termsNorm.Substring(1)); parameterArg = "@" + (parametersForLinqQuery.Count - 1); clause = $"({columnName} != {DataConstants.Null} && {columnName} != \"\" && ({columnName} == {parameterArg} || {columnName}.{ConditionalConstants.StartsWith}({parameterArg})))"; } else { parametersForLinqQuery.Add(termsNorm); parameterArg = "@" + (parametersForLinqQuery.Count - 1); clause = $"({columnName} != {DataConstants.Null} && {columnName} != \"\" && ({columnName} == {parameterArg} || {columnName}.ToLowerInvariant().{ConditionalConstants.Contain}({parameterArg})))"; } return(clause); }
private static string GetFilterClause(string query, DataTablePropertyInfoModel column, List <object> parametersForLinqQuery) { string Clause(string queryPart) { return(Filters .Select(f => f(queryPart, column.PropertyInfo.Name, column, parametersForLinqQuery)) .FirstOrDefault(filterPart => filterPart != null) ?? string.Empty); } var queryParts = query.Split('|').Select(Clause).Where(clause => clause != string.Empty).ToArray(); if (queryParts.Any()) { return("(" + string.Join(") OR (", queryParts) + ")"); } return(null); }
public static string StringFilter(string terms, string columnName, DataTablePropertyInfoModel propertyInfo, List <object> parametersForLinqQuery) { if (terms == ".*") { return(string.Empty); } string parameterArg; if (terms.StartsWith("^")) { if (terms.EndsWith("$")) { parametersForLinqQuery.Add(terms.Substring(1, terms.Length - 2)); parameterArg = $"@{parametersForLinqQuery.Count - 1}"; return($"{columnName} == {parameterArg}"); } parametersForLinqQuery.Add(terms.Substring(1)); parameterArg = "@" + (parametersForLinqQuery.Count - 1); return ($"({columnName} != {DataConstants.Null} && {columnName} != \"\" && ({columnName} == {parameterArg} || {columnName}.{ConditionalConstants.StartsWith}({parameterArg})))"); } parametersForLinqQuery.Add(terms); parameterArg = "@" + (parametersForLinqQuery.Count - 1); return ($"({columnName} != {DataConstants.Null} && {columnName} != \"\" && ({columnName} == {parameterArg} || {columnName}.{ConditionalConstants.StartsWith}({parameterArg}) || {columnName}.{ConditionalConstants.Contain}({parameterArg})))"); }
internal static DataTablePropertyInfoModel[] GetProperties(Type type) { return(PropertiesCache.GetOrAdd(type, t => { var listDataTablePropertyInfo = new List <DataTablePropertyInfoModel>(); foreach (var propertyInfo in t.GetProperties()) { if (propertyInfo.GetCustomAttribute <DataTableIgnoreAttribute>() != null) { // Ignore Property have DataTableIgnoreAttribute continue; } var attributes = propertyInfo.GetCustomAttributes <DataTableBaseAttribute>().ToArray(); var dataTablePropertyInfo = new DataTablePropertyInfoModel(propertyInfo, attributes); listDataTablePropertyInfo.Add(dataTablePropertyInfo); } return listDataTablePropertyInfo.ToArray(); })); }
private static bool IsBoolType(DataTablePropertyInfoModel propertyInfo) { var isBoolType = propertyInfo.Type == typeof(bool) || propertyInfo.Type == typeof(bool?); return(isBoolType); }
private static bool IsEnumType(DataTablePropertyInfoModel propertyInfo) { var isEnumType = propertyInfo.Type.GetNotNullableType().IsEnum; return(isEnumType); }
private static bool IsNumericType(DataTablePropertyInfoModel propertyInfo) { var isNumericType = propertyInfo.Type.IsNumericType(); return(isNumericType); }
/// <summary> /// Filter Enum by Label (Display Name ?? Description ?? Name) with conditional Equals, /// StartsWith, Contains /// </summary> /// <param name="terms"> </param> /// <param name="columnName"> </param> /// <param name="propertyInfo"> </param> /// <param name="parametersForLinqQuery"></param> /// <returns></returns> /// <remarks> /// terms is "null" with Type is Nullable Enum work as search null value /// </remarks> public static string EnumFilter(string terms, string columnName, DataTablePropertyInfoModel propertyInfo, List <object> parametersForLinqQuery) { if (string.IsNullOrWhiteSpace(terms)) { return(null); } var termsNorm = terms.Trim().ToLowerInvariant(); if (termsNorm.StartsWith("^")) { termsNorm = termsNorm.Substring(1); } if (termsNorm.EndsWith("$")) { termsNorm = termsNorm.Substring(0, termsNorm.Length - 1); } if (propertyInfo.Type.IsNullableType() && propertyInfo.Type.IsEnum) { // Enum Nullable type, handle for "null" case ("null" string as null obj) if (DataConstants.Null.Equals(termsNorm, StringComparison.OrdinalIgnoreCase) || string.IsNullOrWhiteSpace(termsNorm)) { return($"{columnName} == {DataConstants.Null}"); } } else { if (string.IsNullOrWhiteSpace(termsNorm)) { return(null); } } var type = propertyInfo.Type.GetNotNullableType(); object enumObject = null; // Search condition for Enum: Equals, StartsWith, Contains foreach (var enumName in Enum.GetNames(type)) { var enumValue = (Enum)TypeDescriptor.GetConverter(type).ConvertFrom(enumName); var valueLowerCase = enumName.ToLowerInvariant(); if (valueLowerCase.Equals(termsNorm, StringComparison.OrdinalIgnoreCase) || valueLowerCase.Contains(termsNorm)) { enumObject = enumValue; // Found, return first found item break; } } // Can't parse string to enum, return null if (enumObject == null) { return(null); } parametersForLinqQuery.Add(enumObject); return($"{columnName} == @{parametersForLinqQuery.Count - 1}"); }
public static string DateTimeFilter(string terms, string columnName, DataTablePropertyInfoModel propertyInfo, List <object> parametersForLinqQuery) { if (string.IsNullOrWhiteSpace(terms)) { return(null); } var termsNorm = terms.Trim().ToLowerInvariant(); if (termsNorm == "~") { return(string.Empty); } string filterString = null; // Range Case if (termsNorm.Contains("~")) { var parts = termsNorm.Split('~'); // FROM DATE TIME var fromDateTime = ToDateTime(parts[0]); if (fromDateTime != default) { parametersForLinqQuery.Add(fromDateTime); filterString = $"{columnName} >= @{parametersForLinqQuery.Count - 1}"; } // TO DATE TIME var toDateTime = ToDateTime(parts[1]); if (toDateTime == default) { return(filterString ?? string.Empty); } parametersForLinqQuery.Add(toDateTime); filterString = (filterString == null ? null : $"{filterString} and ") + $"{columnName} <= @{parametersForLinqQuery.Count - 1}"; return(filterString); } // Single Case var dateTime = ToDateTime(termsNorm); if (dateTime == default) { return(null); } // DateTime only have Date value => search value in same Date if (dateTime.Date == dateTime) { parametersForLinqQuery.Add(dateTime); parametersForLinqQuery.Add(dateTime.AddDays(1)); filterString = $"{columnName} >= @{parametersForLinqQuery.Count - 2} and {columnName} < @{parametersForLinqQuery.Count - 1}"; return(filterString); } // DateTime have Date and Time value => search value in same Date and Time. // If you store DateTime in database include milliseconds => no result match. Ex: in // Database "2017-10-16 10:51:09.9761005 +00:00" so user search "2017-10-16 10:51" will // return 0 result, because not exactly same (even user give full datetime with // milliseconds exactly - this is Linq2SQL issue). // Solution: filter in range of second parametersForLinqQuery.Add(dateTime); parametersForLinqQuery.Add(dateTime.AddSeconds(1)); filterString = $"{columnName} >= @{parametersForLinqQuery.Count - 2} and {columnName} < @{parametersForLinqQuery.Count - 1}"; return(filterString); }
internal static DataTablePropertyInfoModel[] GetProperties(Type type) { return(PropertiesCache.GetOrAdd(type, t => { var listDataTablePropertyInfo = new List <DataTablePropertyInfoModel>(); var listPropertyInfo = t.GetProperties(); // Scan Interface var listInterface = type.GetInterfaces(); var allInterfaceListPropertyInfo = new List <PropertyInfo>(); foreach (var @interface in listInterface) { var interfaceListPropertyInfo = @interface .GetProperties() .Where(x => x.GetCustomAttribute <DataTableBaseAttribute>() != null || x.GetCustomAttribute <DataTableIgnoreAttribute>() != null); allInterfaceListPropertyInfo.AddRange(interfaceListPropertyInfo); } foreach (var propertyInfo in listPropertyInfo) { if (propertyInfo.GetCustomAttribute <DataTableIgnoreAttribute>() != null) { // Ignore Property have DataTableIgnoreAttribute continue; } var attributes = propertyInfo.GetCustomAttributes <DataTableBaseAttribute>().ToArray(); // If Property in Class not have any DataTable Attributes, then find it in the Interface if (!attributes.Any()) { var matchPropertyInfo = allInterfaceListPropertyInfo.FirstOrDefault(x => x.Name == propertyInfo.Name); if (matchPropertyInfo != null) { if (matchPropertyInfo.GetCustomAttribute <DataTableIgnoreAttribute>() != null) { // Ignore Property have DataTableIgnoreAttribute continue; } attributes = matchPropertyInfo.GetCustomAttributes <DataTableBaseAttribute>().ToArray(); } } // Add to List Property var dataTablePropertyInfo = new DataTablePropertyInfoModel(propertyInfo, attributes) { Order = attributes.OfType <DataTableAttribute>().Select(a => a.Order).SingleOrDefault() }; listDataTablePropertyInfo.Add(dataTablePropertyInfo); } // Order listDataTablePropertyInfo = listDataTablePropertyInfo.OrderBy(x => x.Order).ToList(); return listDataTablePropertyInfo.ToArray(); })); }