// compare directory file content with browsed file content private void CompareFile(int i, string[] filesList) { try { string fileContent = GetFileText(filesList[i]); if (!string.IsNullOrEmpty(fileContent.Trim())) { foreach (string text in SourceFileContent) { if (fileContent.Replace(" ", "").ToLower().Trim().Contains(text.Replace(" ", "").ToLower().Trim())) { CompareResult compareResult = new CompareResult(); compareResult.matchText = text; compareResult.filePath = filesList[i]; // set value of chracter tracing compareResult.charTracing = SetCharTracingText(fileContent, text); CompareResultBag.Add(compareResult); } else { // for testing only CompareResult compareResult = new CompareResult(); compareResult.matchText = text; compareResult.filePath = filesList[i]; UnMatchResultBag.Add(compareResult); } } var fileMatchWords = CompareResultBag.Where(x => x.filePath == filesList[i]); if (fileMatchWords != null && fileMatchWords.ToList().Count > 0) { var compareResultsList = fileMatchWords.GroupBy(item => item.matchText) .Select(grp => grp.OrderBy(item => item.matchText).First()) .ToList(); double totalMatchWords = (double)compareResultsList.Count; double fileWordsWithCombination = SourceFileContent.Distinct().ToList().Count; double percentage = Convert.ToDouble(totalMatchWords / fileWordsWithCombination) * 100; fileMatchWords.FirstOrDefault().filePercentage = Math.Round(percentage, 2).ToString() + "%"; } } } catch (Exception ex) { throw ex; } }