private Dictionary <string, Tuple <string, string> > CalculateBeginsAndEnds(string familyBegNumberField, string familyEndNumberField) { IEnumerable <RecreateFamilyEntity> familiesData = DocumentBO.GetBeginDocEndDocForDocument(matterId, _dataset.CollectionId, familyBegNumberField, familyEndNumberField) .OrderByDescending(t => t.FieldValue); Dictionary <string, RecreateFamilyEntity> beginnings = new Dictionary <string, RecreateFamilyEntity>(); // DocId to BEGDOC RecreateFamilyEntity Dictionary <string, string> endings = new Dictionary <string, string>(); // DocId to FieldValue foreach (var recreateFamilyEntity in familiesData) { string[] relation = recreateFamilyEntity.Path.Split('>'); if (recreateFamilyEntity.BeginEndDocState.Equals("BEGDOC")) { //Tracer.Debug(recreateFamilyEntity.ToString()); if (!beginnings.ContainsKey(relation[0])) { beginnings.Add(relation[0], recreateFamilyEntity); } if (!beginnings.ContainsKey(relation[1])) { beginnings.Add(relation[1], recreateFamilyEntity); } } if (recreateFamilyEntity.BeginEndDocState.Equals("ENDDOC")) { //Tracer.Debug(recreateFamilyEntity.ToString()); string rootDocId = recreateFamilyEntity.RootDocId; if (!endings.ContainsKey(rootDocId)) { endings.Add(rootDocId, recreateFamilyEntity.FieldValue); } else { //To set highest ending value in AlphaNumeric character(e.g BAL101,BAL005...) Int64 currentValue; Int64 previousValue; if (!Int64.TryParse(Regex.Replace(recreateFamilyEntity.FieldValue, @"[^\d]", string.Empty), out currentValue) || !Int64.TryParse(Regex.Replace(endings[rootDocId], @"[^\d]", string.Empty), out previousValue)) { continue; } if (currentValue > previousValue) { endings[rootDocId] = recreateFamilyEntity.FieldValue; } } } } //string ending = beginnings[rootDocId]; var beginsEndsMap = new Dictionary <string, Tuple <string, string> >(); foreach (var docId in beginnings.Keys) { RecreateFamilyEntity recreateFamilyEntity = beginnings[docId]; string begin = recreateFamilyEntity.FieldValue; string rootDocId = recreateFamilyEntity.RootDocId; string end = string.Empty; if (endings.ContainsKey(rootDocId)) { end = endings[rootDocId]; } beginsEndsMap.Add(docId, new Tuple <string, string>(begin, end)); //Tracer.Debug("DocId = {0}, Begin = {1}, End = {2}", docId, begin, end); } return(beginsEndsMap); }