public DataTable MapResult(Guid tenantId, string entityName, DataTable source, QueryContext queryModel)
        {
            if (source == null || source.Rows.Count == 0)
            {
                return(source);
            }

            IMetadataManager iMetadataManager = new MetadataManager.Contracts.MetadataManager();
            var subTypes = iMetadataManager.GetSubTypesDetails(entityName);
            IPicklistManager iPicklistManager = new PicklistManager();
            var picklists             = iPicklistManager.GetAllPicklists(tenantId);
            var simpleNonCustomizable = picklists.Where(t => !t.CustomizeValue).ToList();

            if (!simpleNonCustomizable.Any())
            {
                return(source);
            }
            var entity            = iMetadataManager.GetEntitityByName(entityName);
            List <MappedItem> val = new List <MappedItem>();

            if (entity.Fields != null && entity.Fields.Any())
            {
                GetUsedPicklistInEntity(tenantId, simpleNonCustomizable, entity.Fields, val);
            }
            if (entity.VersionControl != null && entity.VersionControl.Fields != null && entity.VersionControl.Fields.Any())
            {
                GetUsedPicklistInEntity(tenantId, simpleNonCustomizable, entity.VersionControl.Fields, val);
            }

            DataTable dtClone = source.Clone(); //just copy structure, no data

            for (int i = 0; i < dtClone.Columns.Count; i++)
            {
                if (dtClone.Columns[i].DataType != typeof(string))
                {
                    dtClone.Columns[i].DataType = typeof(string);
                }
            }

            foreach (DataRow dr in source.Rows)
            {
                dtClone.ImportRow(dr);
            }

            var customFields = new List <FieldModel>();

            // string[] userQuery = null;
            if (queryModel.Fields != null && queryModel.Fields.Any())
            {
                var entityDetails = iMetadataManager.GetEntitityByName(entityName);

                if (entityDetails?.Fields != null && entityDetails.Fields.Any())
                {
                    var userQuery = queryModel?.Fields?.Split(',');
                    foreach (var clientField in userQuery)
                    {
                        var match = entityDetails.Fields.FirstOrDefault(t =>
                                                                        t.Name.ToLower().Equals(clientField.ToString().ToLower()) && !string.IsNullOrEmpty(t.Context) && !string.IsNullOrEmpty(t.ContextType));
                        if (match != null)
                        {
                            if (match.ContextType.Equals("CustomServerFieldBase"))
                            {
                                dtClone.Columns.Add(match.Name, typeof(string));
                                customFields.Add(match);
                            }
                            else if (match.ContextType.Equals("CustomClientFieldBase"))
                            {
                                DataColumn client = new DataColumn(match.Name, typeof(string));
                                //  client.DefaultValue = match.Context;
                                dtClone.Columns.Add(client);
                            }
                        }
                    }
                }
            }

            foreach (DataRow item in dtClone.Rows)
            {
                try
                {
                    var subType = item["SubType"];
                    var mapped  = subTypes.FirstOrDefault(t => t.Key.Equals(subType));
                    if (mapped.Key != null)
                    {
                        item["SubType"] = mapped.Value.ToString();
                    }

                    foreach (var data in val)
                    {
                        if (item.Table.Columns.Contains(data.Name.ToLower()))
                        {
                            if (data.Result != null && data.Result.Rows.Count > 0)
                            {
                                var       actualValue = item[data.Name].ToString();
                                DataRow[] rowsData    = data.Result.Select();
                                for (int i = 0; i < rowsData.Length; i++)
                                {
                                    var existenceValue = rowsData[i]["InternalId"];
                                    if (actualValue.ToString().ToLower().Equals(existenceValue.ToString().ToLower()))
                                    {
                                        var text = rowsData[i]["Text"].ToString();
                                        item[data.Name] = text.ToString();
                                    }
                                }
                            }
                        }
                    }
                    //------------------------- custom client field
                    if (customFields.Any())
                    {
                        foreach (var cf in customFields)
                        {
                            if (item.Table.Columns.Contains(cf.Name))
                            {
                                var conditions = new Dictionary <string, string>();
                                //conditions.Add ("InternalId", item["InternalId"].ToString ());
                                var id = Guid.Parse(item["InternalId"].ToString());
                                item[cf.Name] = GetCustomFieldValue(id, cf.Context, conditions);
                            }
                        }
                    }
                }
                catch
                { //digest exceptions }
                }
            }
            return(dtClone);
        }
        public DataTable GetCustomField(Guid tenantId, string entityName, DataTable source, QueryContext queryModel)
        {
            if (source == null || source.Rows.Count == 0)
            {
                return(source);
            }
            IMetadataManager iMetadataManager = new MetadataManager.Contracts.MetadataManager();
            var subTypes = iMetadataManager.GetSubTypesDetails(entityName);
            IPicklistManager iPicklistManager = new PicklistManager();
            var entity            = iMetadataManager.GetEntitityByName(entityName);
            List <MappedItem> val = new List <MappedItem> ();
            var customFields      = new List <FieldModel> ();

            // string[] userQuery = null;
            if (queryModel.Fields != null && queryModel.Fields.Any())
            {
                var entityDetails = iMetadataManager.GetEntitityByName(entityName);
                if (entityDetails?.Fields != null && entityDetails.Fields.Any())
                {
                    var userQuery = queryModel?.Fields?.Split(',');
                    foreach (var clientField in userQuery)
                    {
                        var match = entityDetails.Fields.FirstOrDefault(t =>
                                                                        t.Name.ToLower().Equals(clientField.ToString().ToLower()) && !string.IsNullOrEmpty(t.Context) && !string.IsNullOrEmpty(t.ContextType));
                        if (match != null)
                        {
                            if (match.ContextType.Equals("CustomServerFieldBase"))
                            {
                                source.Columns.Add(match.Name, typeof(string));
                                customFields.Add(match);
                            }
                            else if (match.ContextType.Equals("CustomClientFieldBase"))
                            {
                                DataColumn client = new DataColumn(match.Name, typeof(string));
                                // client.DefaultValue = match.Context;
                                source.Columns.Add(client);
                            }
                        }
                    }
                }
            }

            foreach (DataRow item in source.Rows)
            {
                try {
                    if (customFields.Any())
                    {
                        foreach (var cf in customFields)
                        {
                            if (item.Table.Columns.Contains(cf.Name))
                            {
                                var conditions = new Dictionary <string, string> ();
                                var id         = Guid.Parse(item["InternalId"].ToString());
                                item[cf.Name] = GetCustomFieldValue(id, cf.Context, conditions);
                            }
                        }
                    }
                } catch { //digest exceptions }
                }
            }
            return(source);
        }