Непрерывный ответ @ Jakotheshadows, но не требующий дополнительных проверок на выходе EF, когда нет ничего, что можно проверить, это ближе к тому, что мы делаем здесь:
// Example lists, a solution for populating will follow
var Names = new List<string> { "Adam", "Joe", "Bob" };
// These two deliberately left blank for demonstration purposes
var specialties = new List<string>();
var ranks = new List<string>();
using(var dbContext = new MyDbContext())
{
var list = dbContext.MyTable
.FilterByNames(names)
.FilterBySpecialties(specialties)
.FilterByRanks(ranks)
.Select(...)
.ToList();
}
Таблица
[Table(...)]
public class MyTable : IMyTable
{
// ...
}
Фильтр по расширениям
public static class MyTableExtensions
{
public static IQueryable<TEntity> FilterMyTablesByName<TEntity>(
this IQueryable<TEntity> query, string[] names)
where TEntity : class, IMyTable
{
if (query == null) { throw new ArgumentNullException(nameof(query)); }
if (!names.Any() || names.All(string.IsNullOrWhiteSpace))
{
return query; // Unmodified
}
// Modified
return query.Where(x => names.Contains(x.Name));
}
// Replicate per array/filter...
}
Кроме того, существуют значительные проблемы с производительностью при использовании Contains (...) или Any (...) внутри запроса EF. Существует более быстрый метод использования Predicate Builders. Это пример с массивом идентификаторов (для этого требуется пакет nuget LinqKit):
public static IQueryable<TEntity> FilterByIDs<TEntity>(
this IQueryable<TEntity> query, int[] ids)
where TEntity : class, IBase
{
if (ids == null || !ids.Any(x => x > 0 && x != int.MaxValue)) { return query; }
return query.AsExpandable().Where(BuildIDsPredicate<TEntity>(ids));
}
private static Expression<Func<TEntity, bool>> BuildIDsPredicate<TEntity>(
IEnumerable<int> ids)
where TEntity : class, IBase
{
return ids.Aggregate(
PredicateBuilder.New<TEntity>(false),
(c, id) => c.Or(p => p.ID == id));
}
Это выводит синтаксис «IN» для очень быстрого запроса:
WHERE ID IN [1,2,3,4,5]
Вот простое регулярное выражение, которое должно работать.
Imports System.Text.RegularExpressions
' ....
Dim reg As New Regex("<a.*?href=(?:'|"")(.+?)(?:'|"").*?>.+?</a>")
Dim input As String = "This is a link: <a href='http://www.stackoverflow.com'>Stackoverflow</a>"
input = reg.Replace(input, "$1", RegexOptions.IgnoreCase)