/// <summary> /// Filters an entry query by entry name. /// </summary> /// <param name="query">Entry query. Cannot be null.</param> /// <param name="nameFilter">Name filter string. If null or empty, no filtering is done.</param> /// <param name="matchMode">Desired mode for matching names.</param> /// <param name="words"> /// List of words for the words search mode. /// Can be null, in which case the words list will be parsed from <paramref name="nameFilter"/>. /// </param> /// <returns>Filtered query. Cannot be null.</returns> /// <typeparam name="TEntry">Entry type.</typeparam> /// <typeparam name="TName">Entry name type.</typeparam> public static IQueryable <TEntry> WhereHasNameGeneric <TEntry, TName>(this IQueryable <TEntry> query, string nameFilter, NameMatchMode matchMode, string[] words = null) where TEntry : IEntryWithNames <TName> where TName : LocalizedStringWithId { if (string.IsNullOrEmpty(nameFilter)) { return(query); } switch (FindHelpers.GetMatchMode(nameFilter, matchMode)) { case NameMatchMode.Exact: return(query.Where(m => m.Names.Names.Any(n => n.Value == nameFilter))); case NameMatchMode.Partial: return(query.Where(m => m.Names.Names.Any(n => n.Value.Contains(nameFilter)))); case NameMatchMode.StartsWith: return(query.Where(m => m.Names.Names.Any(n => n.Value.StartsWith(nameFilter)))); case NameMatchMode.Words: words = words ?? FindHelpers.GetQueryWords(nameFilter); switch (words.Length) { case 1: query = query.Where(q => q.Names.Names.Any(n => n.Value.Contains(words[0]))); break; case 2: query = query.Where(q => q.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Names.Names.Any(n => n.Value.Contains(words[1])) ); break; case 3: query = query.Where(q => q.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Names.Names.Any(n => n.Value.Contains(words[2])) ); break; case 4: query = query.Where(q => q.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Names.Names.Any(n => n.Value.Contains(words[2])) && q.Names.Names.Any(n => n.Value.Contains(words[3])) ); break; case 5: query = query.Where(q => q.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Names.Names.Any(n => n.Value.Contains(words[2])) && q.Names.Names.Any(n => n.Value.Contains(words[3])) && q.Names.Names.Any(n => n.Value.Contains(words[4])) ); break; case 6: query = query.Where(q => q.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Names.Names.Any(n => n.Value.Contains(words[2])) && q.Names.Names.Any(n => n.Value.Contains(words[3])) && q.Names.Names.Any(n => n.Value.Contains(words[4])) && q.Names.Names.Any(n => n.Value.Contains(words[5])) ); break; } return(query); } return(query); }
/// <summary> /// Filters a song link query by a name query. /// </summary> /// <param name="query">Song query. Cannot be null.</param> /// <param name="nameFilter">Name filter string. If null or empty, no filtering is done.</param> /// <param name="matchMode">Desired mode for matching names.</param> /// <param name="words"> /// List of words for the words search mode. /// Can be null, in which case the words list will be parsed from <paramref name="nameFilter"/>. /// </param> /// <returns>Filtered query. Cannot be null.</returns> /// <remarks> /// Note: this code should be optimized after switching to EF. /// Cannot be optimized as is for NH. /// </remarks> public static IQueryable <T> WhereChildHasName <T>(this IQueryable <T> query, string nameFilter, NameMatchMode matchMode, string[] words = null) where T : ISongLink { if (string.IsNullOrEmpty(nameFilter)) { return(query); } switch (FindHelpers.GetMatchMode(nameFilter, matchMode)) { case NameMatchMode.Exact: return(query.Where(m => m.Song.Names.Names.Any(n => n.Value == nameFilter))); case NameMatchMode.Partial: return(query.Where(m => m.Song.Names.Names.Any(n => n.Value.Contains(nameFilter)))); case NameMatchMode.StartsWith: return(query.Where(m => m.Song.Names.Names.Any(n => n.Value.StartsWith(nameFilter)))); case NameMatchMode.Words: words = words ?? FindHelpers.GetQueryWords(nameFilter); switch (words.Length) { case 1: query = query.Where(q => q.Song.Names.Names.Any(n => n.Value.Contains(words[0]))); break; case 2: query = query.Where(q => q.Song.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[1])) ); break; case 3: query = query.Where(q => q.Song.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[2])) ); break; case 4: query = query.Where(q => q.Song.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[2])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[3])) ); break; case 5: query = query.Where(q => q.Song.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[2])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[3])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[4])) ); break; case 6: query = query.Where(q => q.Song.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[2])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[3])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[4])) && q.Song.Names.Names.Any(n => n.Value.Contains(words[5])) ); break; } return(query); } return(query); }
// TODO: should be combined with common name query somehow, but NH is making it hard /// <summary> /// Filters an artist query by a name query. /// </summary> /// <param name="query">Artist query. Cannot be null.</param> /// <param name="nameFilter">Name filter string. If null or empty, no filtering is done.</param> /// <param name="matchMode">Desired mode for matching names.</param> /// <param name="words"> /// List of words for the words search mode. /// Can be null, in which case the words list will be parsed from <paramref name="nameFilter"/>. /// </param> /// <returns>Filtered query. Cannot be null.</returns> public static IQueryable <Album> WhereHasName(this IQueryable <Album> query, string nameFilter, NameMatchMode matchMode, string[] words = null, bool allowCatNum = false) { if (string.IsNullOrEmpty(nameFilter)) { return(query); } switch (FindHelpers.GetMatchMode(nameFilter, matchMode)) { case NameMatchMode.Exact: return(query.Where(m => m.Names.Names.Any(n => n.Value == nameFilter))); case NameMatchMode.Partial: return(query.Where(m => (allowCatNum && m.OriginalRelease.CatNum != null && m.OriginalRelease.CatNum.Contains(nameFilter)) || m.Names.Names.Any(n => n.Value.Contains(nameFilter)))); case NameMatchMode.StartsWith: return(query.Where(m => (allowCatNum && m.OriginalRelease.CatNum != null && m.OriginalRelease.CatNum.StartsWith(nameFilter)) || m.Names.Names.Any(n => n.Value.StartsWith(nameFilter)))); case NameMatchMode.Words: words = words ?? FindHelpers.GetQueryWords(nameFilter); switch (words.Length) { case 1: query = query.Where(q => (allowCatNum && q.OriginalRelease.CatNum != null && q.OriginalRelease.CatNum.Contains(nameFilter)) || q.Names.Names.Any(n => n.Value.Contains(words[0]))); break; case 2: query = query.Where(q => (allowCatNum && q.OriginalRelease.CatNum != null && q.OriginalRelease.CatNum.Contains(nameFilter)) || q.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Names.Names.Any(n => n.Value.Contains(words[1])) ); break; case 3: query = query.Where(q => (allowCatNum && q.OriginalRelease.CatNum != null && q.OriginalRelease.CatNum.Contains(nameFilter)) || q.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Names.Names.Any(n => n.Value.Contains(words[2])) ); break; case 4: query = query.Where(q => (allowCatNum && q.OriginalRelease.CatNum != null && q.OriginalRelease.CatNum.Contains(nameFilter)) || q.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Names.Names.Any(n => n.Value.Contains(words[2])) && q.Names.Names.Any(n => n.Value.Contains(words[3])) ); break; case 5: query = query.Where(q => (allowCatNum && q.OriginalRelease.CatNum != null && q.OriginalRelease.CatNum.Contains(nameFilter)) || q.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Names.Names.Any(n => n.Value.Contains(words[2])) && q.Names.Names.Any(n => n.Value.Contains(words[3])) && q.Names.Names.Any(n => n.Value.Contains(words[4])) ); break; case 6: query = query.Where(q => (allowCatNum && q.OriginalRelease.CatNum != null && q.OriginalRelease.CatNum.Contains(nameFilter)) || q.Names.Names.Any(n => n.Value.Contains(words[0])) && q.Names.Names.Any(n => n.Value.Contains(words[1])) && q.Names.Names.Any(n => n.Value.Contains(words[2])) && q.Names.Names.Any(n => n.Value.Contains(words[3])) && q.Names.Names.Any(n => n.Value.Contains(words[4])) && q.Names.Names.Any(n => n.Value.Contains(words[5])) ); break; } return(query); } return(query); }