public void LoadHints(IEnumerable <MTargetField> targetElements, Histogram histogram)
        {
            //var crit = PredicateBuilder.True<Element>();
            var columnNames = histogram.FieldEntries.ToDictionary(entry => entry.Column.ColumnName.ToUpper().Trim());

            if (HintsApplied)
            {
                return;
            }
            foreach (var target in targetElements)
            {
                var hint = target.MappingHints.FirstOrDefault(columnNames.ContainsKey);
                if (!string.IsNullOrEmpty(hint))
                {
                    MOriginalField mapping = null;

                    if (columnNames.ContainsKey(hint))
                    {
                        mapping = target.OriginalFields
                                  .FirstOrDefault(fld => fld.Name.EqualsIgnoreCase(columnNames[hint].Column.ColumnName));
                    }


                    Mappings[target.Name] = mapping == null ? null : mapping.Name;
                }
            }
            HintsApplied = true;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="OnMappingChangedEventArgs"/> class.
 /// </summary>
 /// <param name="field">The field.</param>
 /// <param name="unMap">if set to <c>true</c> [un map].</param>
 public OnMappingChangedEventArgs(MOriginalField field, bool unMap)
 {
     Field = field;
     UnMap = unMap;
 }
        /// <summary>
        /// Loads the mapping hints.
        /// </summary>
        /// <typeparam name="TWizardDataContextObject">The type of the wizard data context object.</typeparam>
        /// <param name="targetElements">The target elements.</param>
        /// <param name="dataContextObject">The data context object.</param>
        /// <param name="sourceElements">The source elements.</param>
        public void LoadHints2 <TWizardDataContextObject>(IEnumerable <MTargetField> targetElements, TWizardDataContextObject dataContextObject, IEnumerable <MOriginalField> sourceElements)
        {
            try
            {
                if (HintsApplied)
                {
                    return;
                }

                var dataContext = dataContextObject as DatasetContext;
                if (dataContext != null &&
                    ((dataContext.MappingType == MappingType.ExistingDataSet && dataContext.SelectedMappingDatasetId > 0) ||
                     (dataContext.MappingType == MappingType.ExistingFile && !string.IsNullOrEmpty(dataContext.SelectDataMappingFile))))
                {
                    var    dataSetXml = new XmlDocument();
                    string xPathQuery;

                    if (dataContext.MappingType == MappingType.ExistingDataSet)
                    {
                        string mappingXmlToUse = GetMappingFromExistingDataset(dataContext);
                        dataSetXml.LoadXml(mappingXmlToUse);
                        xPathQuery = "/Data/summary/mappings/required/mapping";
                    }
                    else
                    {
                        dataSetXml.Load(dataContext.SelectDataMappingFile);
                        xPathQuery = "/mappings/required/mapping";
                    }

                    var mappingsElement = dataSetXml.SelectNodes(xPathQuery).OfType <XmlNode>().ToList();

                    var columnNames = sourceElements.ToDictionary(entry => entry.Name.ToUpper().Trim());
                    foreach (var target in mappingsElement)
                    {
                        string hint = null;

                        var keyAttribute   = target.Attributes["key"];
                        var valueAttribute = target.Attributes["value"];

                        if (valueAttribute == null || keyAttribute == null)
                        {
                            continue;
                        }

                        if (valueAttribute.Value.ToUpper().In(columnNames.Keys))
                        {
                            hint = valueAttribute.Value.ToUpper();
                        }

                        if (!string.IsNullOrEmpty(hint))
                        {
                            MOriginalField mapping = null;

                            if (columnNames.ContainsKey(hint))
                            {
                                mapping = sourceElements.ToList().FirstOrDefault(fld => fld.Name.EqualsIgnoreCase(columnNames[hint.ToUpper()].Name));
                            }

                            Mappings[keyAttribute.Value] = mapping == null ? null : mapping.Name;
                        }
                    }
                }
                else
                {
                    var columnNames = sourceElements.ToDictionary(entry => entry.Name.ToUpper().Trim());
                    foreach (var target in targetElements)
                    {
                        string hint = null;

                        foreach (var mapHint in target.MappingHints.ToList())
                        {
                            if (mapHint.ToUpper().In(columnNames.Keys))
                            {
                                hint = mapHint.ToUpper();
                                break;
                            }
                        }

                        if (string.IsNullOrEmpty(hint))
                        {
                            continue;
                        }

                        MOriginalField mapping = null;

                        if (columnNames.ContainsKey(hint))
                        {
                            mapping = sourceElements.ToList().FirstOrDefault(fld => fld.Name.EqualsIgnoreCase(columnNames[hint.ToUpper()].Name));
                        }

                        Mappings[target.Name] = mapping == null ? null : mapping.Name;
                    }

                    #region Old code. It can be removed but after QA resolves issue. This is for historical reference at the moment - Jason

                    //var hint = target.MappingHints.FirstOrDefault(h => columnNames.Keys.Any(key => key.EqualsIgnoreCase(h) ||
                    //                                                                        key.StartsWith(h, StringComparison.OrdinalIgnoreCase) ||
                    //                                                                        key.EndsWith(h, StringComparison.OrdinalIgnoreCase)));
                    //if (!string.IsNullOrEmpty(hint))
                    //{
                    //    var mapping = sourceElements.FirstOrDefault(fld =>
                    //    {
                    //        var column = columnNames.FirstOrDefault(h => h.Key.EqualsIgnoreCase(hint) ||
                    //                                                                        h.Key.StartsWith(hint, StringComparison.OrdinalIgnoreCase) ||
                    //                                                                        h.Key.EndsWith(hint, StringComparison.OrdinalIgnoreCase));

                    //        if (column.Value != null)
                    //            return fld.Name.StartsWith(column.Value.Column.ColumnName, StringComparison.InvariantCulture);
                    //        else
                    //            return false;
                    //        //return fld.Name.StartsWith(columnNames[hint].Column.ColumnName, StringComparison.InvariantCulture);
                    //    });

                    //   Mappings[target.Name] = mapping == null ? null : mapping.Name;
                    //}

                    #endregion
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                HintsApplied = true;
            }
        }