public Series FindByTitleInexact(string title) { // find any series clean title within the provided release title string cleanTitle = title.CleanSeriesTitle(); var list = _seriesRepository.FindByTitleInexact(cleanTitle); if (!list.Any()) { // no series matched return(null); } if (list.Count == 1) { // return the first series if there is only one return(list.Single()); } // build ordered list of series by position in the search string var query = list.Select(series => new { position = cleanTitle.IndexOf(series.CleanTitle), length = series.CleanTitle.Length, series = series }) .Where(s => (s.position >= 0)) .ToList() .OrderBy(s => s.position) .ThenByDescending(s => s.length) .ToList(); // get the leftmost series that is the longest // series are usually the first thing in release title, so we select the leftmost and longest match var match = query.First().series; _logger.Debug("Multiple series matched {0} from title {1}", match.Title, title); foreach (var entry in list) { _logger.Debug("Multiple series match candidate: {0} cleantitle: {1}", entry.Title, entry.CleanTitle); } return(match); }