I want to encapsulate common scenarios when using EF6. Here's an example:
public class StringRequest : DbRequestProperty
{
public string Name { get; set; }
public bool? ExactMatch { get; set; }
protected override bool IsValid()
{
return !string.IsNullOrWhiteSpace(Name);
}
private bool RequestExactMatch()
{
return ExactMatch.HasValue && ExactMatch.Value;
}
protected override IQueryable<T> Execute<T>(IQueryable<T> original, string propertyName)
{
return RequestExactMatch()
? original.Where(o => GetProperty<string>(o, propertyName) == Name)
: original.Where(o => GetProperty<string>(o, propertyName).Contains(Name));
}
}
But GetProperty can't be converted to a query. So I'm thinking on selecting dynamically the column using "propertyName".
protected override IQueryable<T> Execute<T>(IQueryable<T> original, string propertyName)
{
return RequestExactMatch()
? original.Where(o => GetColumnByName<string>(propertyName) == Name)
: original.Where(o => GetColumnByName<string>(propertyName).Contains(Name));
}
Is this possible? Thanks in advance.