Created
March 31, 2016 02:34
-
-
Save petarvucetin/8ea207365132ace9688ca19bc1d7bb8c to your computer and use it in GitHub Desktop.
Useful extension methods for strongly typed access to CRM Entites
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
public static class CrmEntityExtensions | |
{ | |
public static OptionSetValue ToOptionSetValue(this Enum e) | |
{ | |
if (e.GetTypeCode() != TypeCode.Int32) | |
return null; | |
var val = (int)Convert.ChangeType(e, e.GetTypeCode()); | |
return new OptionSetValue(val); | |
} | |
public static void Copy<T>(this Entity entity, params KeyValuePair<Expression<Func<T, object>>, object>[] values) | |
{ | |
foreach (var expression in values) | |
{ | |
SetOrAddAttributeValue(entity, expression.Key, expression.Value); | |
} | |
} | |
public static K GetAttributeValue<T, K>(this Entity entity, Expression<Func<T, object>> attributeLogicalName) | |
{ | |
var column = GetPropertyName(attributeLogicalName).ToLowerInvariant(); | |
if (!entity.Attributes.ContainsKey(column)) | |
return default(K); | |
return (K)entity.Attributes[column]; | |
} | |
public static void SetOrAddAttributeValue<T>(this Entity entity, Expression<Func<T, object>> attributeLogicalName, object value) | |
{ | |
var column = GetPropertyName<T>(attributeLogicalName).ToLowerInvariant(); | |
if (!entity.Attributes.ContainsKey(column)) | |
{ | |
entity.Attributes.Add(column, value); | |
} | |
else | |
{ | |
entity.Attributes[column] = value; | |
} | |
} | |
public static void AddCondition<T>(this FilterExpression filter, Expression<Func<T, object>> exp, ConditionOperator conditionOperator, params object[] values) | |
{ | |
var column = GetPropertyName<T>(exp).ToLowerInvariant(); | |
filter.AddCondition(column, conditionOperator, values); | |
} | |
public static void AddColumns<T>(this ColumnSet columnSet, params Expression<Func<T, object>>[] exp) | |
{ | |
foreach (var expression in exp) | |
{ | |
var column = GetPropertyName<T>(expression).ToLowerInvariant(); | |
columnSet.AddColumn(column); | |
} | |
} | |
public static string GetPropertyName<T>(System.Linq.Expressions.Expression<Func<T, object>> property) | |
{ | |
System.Linq.Expressions.LambdaExpression lambda = property; | |
System.Linq.Expressions.MemberExpression memberExpression; | |
if (lambda.Body is System.Linq.Expressions.UnaryExpression) | |
{ | |
System.Linq.Expressions.UnaryExpression unaryExpression = (System.Linq.Expressions.UnaryExpression)(lambda.Body); | |
memberExpression = (System.Linq.Expressions.MemberExpression)(unaryExpression.Operand); | |
} | |
else | |
{ | |
memberExpression = (System.Linq.Expressions.MemberExpression)(lambda.Body); | |
} | |
return ((PropertyInfo)memberExpression.Member).Name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment