我想在使用EF6时封装常见的场景.这是一个例子:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
但GetProperty无法转换为查询.所以我正在考虑使用"propertyName"动态选择列.
protected override IQueryable<T> Execute<T>(IQueryable<T> original, string propertyName)
{
return RequestExactMatch()
? original.Where(o => GetColumnByName<string>(propertyName) == Name)
: …Run Code Online (Sandbox Code Playgroud)