【发布时间】:2015-09-26 22:13:27
【问题描述】:
我想封装使用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));
}
}
但 GetProperty 无法转换为查询。 所以我正在考虑使用“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));
}
这可能吗? 提前致谢。
【问题讨论】:
标签: c# entity-framework linq-to-entities entity-framework-6