Hi All.
I have a filters with value:-
"{\"groupOp\":\"AND\",\"rules\":[],\"groups\":[{\"groupOp\":\"OR\",\"groups\":[],\"rules\":[{\"data\":\"Aborted\",\"op\":\"eq\",\"field\":\"Contract_Status\"},{\"data\":\"Completed\",\"op\":\"eq\",\"field\":\"Contract_Status\"}]},{\"groupOp\":\"OR\",\"groups\":[],\"rules\":[]}]}"
tringBuilder sb = new StringBuilder(); string rtRule = string.Empty; string rtWhere = string.Empty; FilterModel filterModel = JsonConvert.DeserializeObject<FilterModel>(filters.ToString()); // How to convert the rules structure to the search criteria structure var searchCriterias = filterModel.rules.Select(Rule => new SearchCriteria { Column = Rule.field, Operation = (WhereOperation) StringEnum.Parse( typeof(WhereOperation), Rule.op), Value = Rule.data }).ToArray();
But, the filterModel > rules return 0 count array.
My FilterModel as
using System; using System.Collections.Generic; using System.Reflection; namespace XXXXX { public static class LinqDynamicConditionHelper { private static Dictionary<string, string> WhereOperation = new Dictionary<string, string> { { "eq", "{1} = {2}({0})" }, { "ne", "{1} != {2}({0})" }, { "lt", "{1} < {2}({0})" }, { "le", "{1} <= {2}({0})" }, { "gt", "{1} > {2}({0})" }, { "ge", "{1} >= {2}({0})" }, { "bw", "{1}.StartsWith({2}({0}))" }, { "bn", "!{1}.StartsWith({2}({0}))" }, { "in", "" }, { "ni", "" }, { "ew", "{1}.EndsWith({2}({0}))" }, { "en", "!{1}.EndsWith({2}({0}))" }, { "cn", "{1}.Contains({2}({0}))" }, { "nc", "!{1}.Contains({2}({0}))" }, { "nu", "{1} == null" }, { "nn", "{1} != null" } }; private static Dictionary<string, string> ValidOperators = new Dictionary<string, string> { { "Object" , "" }, { "Boolean" , "eq:ne:" }, { "Char" , "" }, { "String" , "eq:ne:lt:le:gt:ge:bw:bn:cn:nc:" }, { "SByte" , "" }, { "Byte" , "eq:ne:lt:le:gt:ge:" }, { "Int16" , "eq:ne:lt:le:gt:ge:" }, { "UInt16" , "" }, { "Int32" , "eq:ne:lt:le:gt:ge:" }, { "UInt32" , "" }, { "Int64" , "eq:ne:lt:le:gt:ge:" }, { "UInt64" , "" }, { "Decimal" , "eq:ne:lt:le:gt:ge:" }, { "Single" , "eq:ne:lt:le:gt:ge:" }, { "Double" , "eq:ne:lt:le:gt:ge:" }, { "DateTime" , "eq:ne:lt:le:gt:ge:" }, { "TimeSpan" , "" }, { "Guid" , "" } }; public static string GetCondition<T>(RuleModel rule) { if (rule.data == "%") { // returns an empty string when the data is ‘%’ return ""; } else { // initializing variables Type myType = null; string myTypeName = string.Empty; string myTypeRawName = string.Empty; PropertyInfo myPropInfo = typeof(T).GetProperty(rule.field.Split('.')[0]); int index = 0; // navigating fields hierarchy foreach (string wrkField in rule.field.Split('.')) { if (index > 0) { myPropInfo = myPropInfo.PropertyType.GetProperty(wrkField); } index++; } // property type and its name myType = myPropInfo.PropertyType; myTypeName = myPropInfo.PropertyType.Name; myTypeRawName = myTypeName; // handling ‘nullable’ fields if (myTypeName.ToLower() == "nullable`1") { myType = Nullable.GetUnderlyingType(myPropInfo.PropertyType); myTypeName = myType.Name + "?"; } // creating the condition expression if (ValidOperators[myTypeRawName].Contains(rule.op + ":")) { string expression = String.Format(WhereOperation[rule.op], GetFormattedData(myType, rule.data), rule.field, myTypeName); return expression; } else { // un-supported operator return ""; } } } //private static string GetFormattedData(Type type, string value) //{ // switch (type.Name.ToLower()) // { // case "string": // value = @"""" + value + @""""; // break; // case "datetime": // DateTime dt = DateTime.Parse(value); // value = dt.Year.ToString() + "," + dt.Month + "," + dt.Day.ToString(); // break; // } // return value; //} private static string GetFormattedData(Type type, string value) { switch (type.Name.ToLower()) { case "string": value = @"""" + value + @""""; break; case "datetime": DateTime dt = DateTime.Parse(value); value = dt.Year.ToString() + "," + dt.Month.ToString() + "," + dt.Day.ToString(); break; } return value; } } public class RuleModel { public string field { get; set; } public string op { get; set; } public string data { get; set; } public string groupOp { get; set; } } public class FilterModel { public string groupOp { get; set; } public IList<RuleModel> rules { get; set; } } }
Please advise,
Thanks.
Regards,
Micheale