private static Dictionary <string, object> GetVariablesValues(List <NotificationVariable> notificationVariables, Guid entityId,
                                                                      string relationEntityName, ref Guid relationId, Dictionary <string, object> oldValue, int languageId)
        {
            DbContext context = DynamicModelBuilder.CreateDataSource();
            Dictionary <string, object> values       = new Dictionary <string, object>();
            NotificationVariable        rootVariable = notificationVariables.Where(c => c.ParentId == Guid.Empty).First();

            ProcessOneVariable(rootVariable, entityId, context, notificationVariables, values, relationEntityName, ref relationId, oldValue, languageId);
            return(values);
        }
        private static void ProcessOneVariable(NotificationVariable variableNode, Guid entityId, DbContext context, List <NotificationVariable> notificationVariables,
                                               Dictionary <string, object> values, string relationEntityName,
                                               ref Guid relationId, Dictionary <string, object> oldValue, int languageId)
        {
            var  entityInfo = MetadataProvider.Instance.EntiyMetadata.Where(c => c.EntityId == variableNode.EntityId).First();
            Type entityType = DynamicTypeBuilder.Instance.GetDynamicType(entityInfo.PhysicalName);

            if (!string.IsNullOrEmpty(relationEntityName) && relationEntityName == entityInfo.PhysicalName)
            {
                relationId = entityId;
            }
            IQueryable     query          = context.Set(entityType).AsQueryable();
            BinaryOperator binaryOperator = new BinaryOperator();

            binaryOperator.OperatorType = BinaryOperatorType.Equal;
            binaryOperator.LeftOperand  = ConstantValue.Parse(entityInfo.Attributes.Where(c => c.IsPKAttribute == true).First().TableColumnName);
            binaryOperator.RightOperand = entityId;
            System.Linq.Expressions.Expression exception = GetExpression(query, binaryOperator);
            query = query.Provider.CreateQuery(exception);
            IEnumerator iter          = query.GetEnumerator();
            object      currentObject = null;
            int         iterIndex     = 0;

            while (iter.MoveNext())
            {
                if (iter.Current != null && iterIndex == 0)
                {
                    currentObject = iter.Current;
                }
                iterIndex++;
            }
            if (currentObject == null)
            {
                return;
            }
            Dictionary <string, object> properValueList = new Dictionary <string, object>();

            foreach (var att in entityInfo.Attributes)
            {
                System.Reflection.PropertyInfo pro = currentObject.GetType().GetProperty(att.PhysicalName);
                if (pro == null)
                {
                    continue;
                }
                var proValue = pro.GetValue(currentObject, null);
                if (att.OptionSet != null)
                {
                    if (proValue != null && !string.IsNullOrEmpty(proValue.ToString()))
                    {
                        var picValue   = att.OptionSet.AttributePicklistValues.Where(c => c.Value == int.Parse(proValue.ToString())).First();
                        var labelValue = MetadataProvider.Instance.LocalizedLabels.Where(c => c.ObjectId == picValue.AttributePicklistValueId && c.LanguageId == languageId);
                        if (labelValue.Count() <= 0)
                        {
                            labelValue = MetadataProvider.Instance.LocalizedLabels.Where(c => c.ObjectId == picValue.AttributePicklistValueId);
                        }
                        var lable = labelValue.First();
                        proValue = lable.Label;
                    }

                    if (oldValue != null && oldValue.Keys.Contains(att.PhysicalName))
                    {
                        var oldV = oldValue[att.PhysicalName];
                        if (oldV != null && !string.IsNullOrEmpty(oldV.ToString()))
                        {
                            var picValue   = att.OptionSet.AttributePicklistValues.Where(c => c.Value == int.Parse(oldV.ToString())).First();
                            var labelValue = MetadataProvider.Instance.LocalizedLabels.Where(c => c.ObjectId == picValue.AttributePicklistValueId && c.LanguageId == languageId);
                            if (labelValue.Count() <= 0)
                            {
                                labelValue = MetadataProvider.Instance.LocalizedLabels.Where(c => c.ObjectId == picValue.AttributePicklistValueId);
                            }
                            var lable = labelValue.First();
                            properValueList.Add("Old" + att.PhysicalName, lable.Label);
                        }
                    }
                }
                properValueList.Add(att.PhysicalName, proValue);
            }
            values.Add(entityInfo.PhysicalName, properValueList);
            var subVariables = notificationVariables.Where(c => c.ParentId == variableNode.NotificationVariableId);

            foreach (var subNode in subVariables)
            {
                var relatedAtt = entityInfo.Attributes.Where(c => c.AttributeId == subNode.RelatedAttributeId).FirstOrDefault();
                if (relatedAtt == null)
                {
                    continue;
                }
                Guid subEntityId = (Guid)properValueList[relatedAtt.PhysicalName];
                ProcessOneVariable(subNode, subEntityId, context, notificationVariables, properValueList, relationEntityName, ref relationId, oldValue, languageId);
            }
        }