/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="poco">If Null Default Value Will Be Used For Members</param> /// <returns>A List Of Advance Members Of T</returns> public static List <AdvanceMember> GetAdvanceMembersForDynamic <T>(T poco = null) where T : class { if (poco == null) { throw new BadCodeException($"Can Get The Properties Of The Dynamic Type {typeof(T).FullName} Because No Properties Has Been Assigned to the object"); } var list = new List <AdvanceMember>() { }; var props = DynamicObjectHelper.GetProperties(poco as ExpandoObject); props.ForEach(delegate(KeyValuePair <string, object> pair) { var advance = new AdvanceMember { Member = new DynamicMember() { Type = pair.Value.GetType(), Name = pair.Key, CanWrite = true, CanRead = true }, Value = pair.Value, SqlCustomAttritube = new SqlColumnAttritube(), Validation = new DataValidationAttritube(), }; list.Add(advance); }); return(list); }
public Tuple <List <DbParameter>, string> BuildWhereClause(Func <string, object, DbParameter> GetNewParameter, ExpandoObject parameters, string additionalWhere = null) { var whereClause = $" WHERE "; var startsWithWhere = (!string.IsNullOrEmpty(additionalWhere) && additionalWhere.Replace(" ", "").StartsWith("WHERE", StringComparison.OrdinalIgnoreCase)); var endWithAnd = (!string.IsNullOrEmpty(additionalWhere) && additionalWhere.Replace(" ", "").EndsWith("AND", StringComparison.OrdinalIgnoreCase)); var startsWithAnd = (!string.IsNullOrEmpty(additionalWhere) && additionalWhere.Replace(" ", "").StartsWith("AND", StringComparison.OrdinalIgnoreCase)); var list = new List <DbParameter>() { }; var dictionary = DynamicObjectHelper.GetProperties(parameters); if (dictionary != null && dictionary.Any()) { dictionary.ForEach(delegate(KeyValuePair <string, object> pair) { if (pair.Value.GetType().IsTypeIEnumerable() && pair.Value.GetType() != typeof(string)) { whereClause += $@"{pair.Key} IN ("; //if (pair.Value is IEnumerable<DateTime> dates) //{ // if (dates.IsNullOrEmpty()) // { // whereClause = whereClause.ReplaceLastOccurrence($@" {pair.Key} IN (", string.Empty, StringComparison.OrdinalIgnoreCase); // } // else // { // for (var i = 0; i < (dates ?? throw new InvalidOperationException()).Count(); i++) // { // whereClause += $"@{pair.Key + i}, "; // list.Add(GetNewParameter(pair.Key + i, dates.ToList()[i])); // } // whereClause = whereClause.ReplaceLastOccurrence(",", ") AND ", StringComparison.OrdinalIgnoreCase); // } //} var tempList = pair.Value as IEnumerable <object>; if (tempList.IsNullOrEmpty()) { whereClause = whereClause.ReplaceLastOccurrence($@" {pair.Key} IN (", string.Empty, StringComparison.OrdinalIgnoreCase); } else { for (var i = 0; i < (tempList ?? throw new InvalidOperationException()).Count(); i++) { whereClause += $"@{pair.Key + i}, "; list.Add(GetNewParameter(pair.Key + i, tempList.ToList()[i])); } whereClause = whereClause.ReplaceLastOccurrence(",", ") AND ", StringComparison.OrdinalIgnoreCase); } } else { whereClause += $@"{pair.Key} = @{pair.Key} AND "; list.Add(GetNewParameter(pair.Key, pair.Value)); } }); whereClause = whereClause.ReplaceLastOccurrence("AND", "", StringComparison.OrdinalIgnoreCase); if (startsWithWhere) { additionalWhere = additionalWhere.ReplaceFirstOccurrence("WHERE", " ", StringComparison.OrdinalIgnoreCase); } if (endWithAnd) { additionalWhere = additionalWhere.ReplaceLastOccurrence("AND", "", StringComparison.OrdinalIgnoreCase); } if (startsWithAnd) { // additionalWhere = additionalWhere.ReplaceLastOccurrence("AND", "", StringComparison.OrdinalIgnoreCase); } else { if (!string.IsNullOrEmpty(additionalWhere)) { additionalWhere = $" AND {additionalWhere}"; } } whereClause += additionalWhere; } return(new Tuple <List <DbParameter>, string>(list, whereClause)); }