我遇到了类似的问题: LINQ to Entities无法识别方法'System.String ToString()'方法,并且此方法无法转换为存储表达式
我正在尝试对我的源进行分页,但在我的情况下,我不能将结果GetPropertyValue放在变量中,因为我需x要这样做:
public IEnumerable<TModel> Paginate(IQueryable<TModel> source, ref int totalPages, int pageIndex, int pageSize, string sortfield, SortDirection? sortdir)
{
totalPages = (int)Math.Ceiling(source.Count() / (double)pageSize);
if (sortdir == SortDirection.Descending)
{
return source.OrderByDescending(x => GetPropertyValue(x, sortfield)).Skip(pageIndex * pageSize).Take(pageSize).ToList();
}
else
{
return source.OrderBy(x => GetPropertyValue(x, sortfield)).Skip(pageIndex * pageSize).Take(pageSize).ToList();
}
}
private static object GetPropertyValue(object obj, string name)
{
return obj == null ? null : obj.GetType().GetProperty(name).GetValue(obj, null);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我该怎么办?