我正在尝试推广API的分页调用。如果我从网页传递过滤器,我希望能够根据页码和页面大小返回一系列项目。如果OrderBy使用set参数或者可能对每个调用都使用丑陋的switch语句,那么这很简单。
目前我正在尝试使用表达式和反射扩展IQueryable(我没有经验)我发现了几个例子,如果我在根类中使用一个简单类型的属性,它就可以工作字符串“FixValidatedCount”)。如果我尝试通过嵌套的类属性(如下面的Island.Name示例)进行排序,我就无法工作。
有没有办法更新我的表达式以接受嵌套的类/属性?或者有更好的方法吗?
[Route("api/issues/paged")]
[HttpPost]
public HttpResponseMessage GetIssuesPage(EntityPageFilter filter)
{
//THE BELOW COMMENTED OUT OBJECT IS THE PARAMETER
//var filter = new EntityPageFilter
//{
// PageSize = 5,
// PageNumber = 1,
// OrderBy = "Island.Name",
// OrderByAscending = true
//};
var query = _issueService.GetIssues()
.OrderByField(filter.OrderBy, filter.OrderByAscending)
//.OrderBy(i => i.Island.Name) THIS WORKS, BUT HOW DO I DO THIS WITH A STRING
.Skip(filter.Skip).Take(filter.PageSize);
return Request.CreateResponse(HttpStatusCode.OK, new PagedEntity<IssueSummary>
{
PageNumber = filter.PageNumber,
PageSize = filter.PageSize,
ItemCount = _issueService.GetIssues().Count(),
Data = query
});
}
IssueSummary是查询的返回类型。
public class Issue
{
public int Id { get; set; }
public Island Island { get; set; }
public Type Type { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? FixedAt { get; set; }
}
public class IssueSummary: Issue
{
public int FixCount { get; set; }
public int FixValidatedCount { get; set; }
}
public class Island
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
IQueryable的扩展方法
public static IQueryable<T> OrderByField<T>(this IQueryable<T> q, string sortField, bool ascending)
{
var properties = sortField.Split('.').ToList();
var xType = typeof(T);
for (var i = 0; i < properties.Count - 1; i++)
{
xType = xType.GetProperty(properties[i]).PropertyType;
}
var param = Expression.Parameter(xType, String.Empty);
var prop = Expression.Property(param, properties.Last());
var exp = Expression.Lambda(prop, param);
var method = ascending ? "OrderBy" : "OrderByDescending";
//var types = new[] { q.ElementType, exp.Body.Type };
var types = new[] { xType, exp.Body.Type };
var mce = Expression.Call(typeof(Queryable), method, types, q.Expression, exp);
return q.Provider.CreateQuery<T>(mce);
}
答案 0 :(得分:3)
获取具有嵌套支持的属性/字段访问器表达式的最简单方法是这样的:
var root = Expression.Parameter(typeof(T), "x");
var member = sortField.Split('.').Aggregate((Expression)root, Expression.PropertyOrField);
var selector = Expression.Lambda(member, root);
以下是完整的扩展方法:
public static IQueryable<T> OrderByField<T>(this IQueryable<T> source, string sortField, bool ascending)
{
var root = Expression.Parameter(typeof(T), "x");
var member = sortField.Split('.').Aggregate((Expression)root, Expression.PropertyOrField);
var selector = Expression.Lambda(member, root);
var method = ascending ? "OrderBy" : "OrderByDescending";
var types = new[] { typeof(T), member.Type };
var mce = Expression.Call(typeof(Queryable), method, types,
source.Expression, Expression.Quote(selector));
return source.Provider.CreateQuery<T>(mce);
}