in apps/samplecode/education/rosterapi/EducationAccelerator.WebApi/EducationAccelerator.WebApi/Models/BaseModel.cs [176:213]
public static string ParseFilter(Type modelType, string filter, Dictionary<string, string> attributes)
{
var match = filterMatcher.Match(filter);
if (!match.Success)
{
throw new InvalidFilterFieldException($"Filter: {filter}");
}
var dataFieldRaw = match.Groups[1].Value;
var dataField = dataFieldRaw.ToLower();
var predicate = match.Groups[2].Value;
var value = match.Groups[3].Value.ToLower();
// get the CRM field name
// get the comparator
// if the field is an enum, get the numerical value, otherwise just pass in the given value (any other types need special consideration?)
var lookupMethod = modelType.GetMethod("LookupProperty", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
try
{
var comparator = ComparatorMap[predicate];
var modelProp = (PropertyInfo)lookupMethod.Invoke(null, new object[] { modelType, dataField });
var fieldType = modelProp.PropertyType;
var getter = modelProp.GetMethod;
var comparisonValue = fieldType.IsEnum ? ((int)Enum.Parse(fieldType, value)).ToString() : value;
comparisonValue = comparator == "like" ? $"%{comparisonValue}%" : comparisonValue;
return $"<condition attribute='{attributes[dataField]}' operator='{comparator}' value='{comparisonValue}' />";
}
catch (Exception e)
{
throw new InvalidFilterFieldException($"{dataFieldRaw}");
}
}