Last active
July 31, 2018 08:46
-
-
Save AmirOfir/9e5ea1fb2b522fbc3ba3d3fdadcb547d to your computer and use it in GitHub Desktop.
Microsoft XRM Filter ConditionOperator to XML operators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using Microsoft.Xrm.Sdk.Query; | |
public static class XmlConditionOperator | |
{ | |
// Credit for BCA https://stackoverflow.com/a/41618045/1238848 | |
public static Dictionary<string, string> GetAllPublicConstantValues(this Type type) | |
{ | |
return type | |
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) | |
.Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(string)) | |
.ToDictionary(x => x.Name, x => x.GetRawConstantValue().ToString()); | |
} | |
public static Dictionary<TValue, TKey> FlipDictionary<TKey, TValue>(this Dictionary<TKey, TValue> dic) | |
=> dic.ToDictionary(kp => kp.Value, kp => kp.Key); | |
private readonly static Dictionary<string, string> _allConstantValues = typeof(XmlConditionOperator).GetAllPublicConstantValues(); | |
private readonly static Dictionary<string, string> _allConstantValuesFlip = _allConstantValues.FlipDictionary(); | |
public static ConditionOperator ToConditionOperator(string str) | |
{ | |
var fieldName = _allConstantValuesFlip[str]; | |
return (ConditionOperator)Enum.Parse(typeof(ConditionOperator), fieldName); | |
} | |
public static string ToXmlOperator(ConditionOperator conditionOperator) | |
{ | |
return _allConstantValues[conditionOperator.ToString()]; | |
} | |
public const string Equal = "eq"; | |
public const string NotEqual = "neq"; | |
public const string GreaterThan = "gt"; //"ne" | |
public const string LessThan = "lt"; | |
public const string GreaterEqual = "ge"; | |
public const string LessEqual = "le"; | |
public const string Like = "like"; | |
public const string NotLike = "not-like"; | |
public const string In = "in"; | |
public const string NotIn = "not-in"; | |
public const string Between = "between"; | |
public const string NotBetween = "not-between"; | |
public const string Null = "null"; | |
public const string NotNull = "not-null"; | |
public const string Yesterday = "yesterday"; | |
public const string Today = "today"; | |
public const string Tomorrow = "tomorrow"; | |
public const string Last7Days = "last-seven-days"; | |
public const string Next7Days = "next-seven-days"; | |
public const string LastWeek = "last-week"; | |
public const string ThisWeek = "this-week"; | |
public const string NextWeek = "next-week"; | |
public const string LastMonth = "last-month"; | |
public const string ThisMonth = "this-month"; | |
public const string NextMonth = "next-month"; | |
public const string On = "on"; | |
public const string OnOrBefore = "on-or-before"; | |
public const string OnOrAfter = "on-or-after"; | |
public const string LastYear = "last-year"; | |
public const string ThisYear = "this-year"; | |
public const string NextYear = "next-year"; | |
public const string LastXHours = "last-x-hours"; | |
public const string NextXHours = "next-x-hours"; | |
public const string LastXDays = "last-x-days"; | |
public const string NextXDays = "next-x-days"; | |
public const string LastXWeeks = "last-x-weeks"; | |
public const string NextXWeeks = "next-x-weeks"; | |
public const string LastXMonths = "last-x-months"; | |
public const string NextXMonths = "next-x-months"; | |
public const string LastXYears = "last-x-years"; | |
public const string NextXYears = "next-x-years"; | |
public const string EqualUserId = "eq-userid"; | |
public const string NotEqualUserId = "ne-userid"; | |
public const string EqualBusinessId = "eq-businessid"; | |
public const string NotEqualBusinessId = "ne-businessid"; | |
//public const string ChildOf = //; | |
//public const string Mask = ; | |
//public const string NotMask = | |
//public const string MasksSelect = ; | |
public const string Contains = "contains"; | |
public const string DoesNotContain = "does-not-contain"; | |
public const string EqualUserLanguage = "eq-userlanguage"; | |
//public const string NotOn = // | |
public const string OlderThanXMonths = "olderthan-x-months"; | |
public const string BeginsWith = "begins-with"; | |
public const string DoesNotBeginWith = "not-begin-with"; | |
public const string EndsWith = "ends-with"; | |
public const string DoesNotEndWith = "not-end-with"; | |
public const string ThisFiscalYear = "this-fiscal-year"; | |
public const string ThisFiscalPeriod = "this-fiscal-period"; | |
public const string NextFiscalYear = "next-fiscal-year"; | |
public const string NextFiscalPeriod = "next-fiscal-period"; | |
public const string LastFiscalYear = "last-fiscal-year"; | |
public const string LastFiscalPeriod = "last-fiscal-period"; | |
public const string LastXFiscalYears = "last-x-fiscal-years"; | |
public const string LastXFiscalPeriods = "last-x-fiscal-periods"; | |
public const string NextXFiscalYears = "next-x-fiscal-years"; | |
public const string NextXFiscalPeriods = "next-x-fiscal-periods"; | |
public const string InFiscalYear = "in-fiscal-year"; | |
public const string InFiscalPeriod = "in-fiscal-period"; | |
public const string InFiscalPeriodAndYear = "in-fiscal-period-and-year"; | |
public const string InOrBeforeFiscalPeriodAndYear = "in-or-before-fiscal-period-and-year"; | |
public const string InOrAfterFiscalPeriodAndYear = "in-or-after-fiscal-period-and-year"; | |
public const string EqualUserTeams = "eq-userteams"; | |
public const string EqualUserOrUserTeams = "eq-useroruserteams"; | |
public const string Under = "under"; | |
public const string NotUnder = "not-under"; | |
public const string UnderOrEqual = "eq-or-under"; | |
public const string Above = "above"; | |
public const string AboveOrEqual = "eq-or-above"; | |
public const string EqualUserOrUserHierarchy = "eq-useroruserhierarchy"; | |
public const string EqualUserOrUserHierarchyAndTeams = "eq-useroruserhierarchyandteams"; | |
public const string OlderThanXYears = "olderthan-x-years"; | |
public const string OlderThanXWeeks = "olderthan-x-weeks"; | |
public const string OlderThanXDays = "olderthan-x-days"; | |
public const string OlderThanXHours = "olderthan-x-hours"; | |
public const string OlderThanXMinutes = "olderthan-x-minutes"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment