这种情况类似于Better way to Sort a List by any property,但在这种情况下,在执行方法'.ToList()'之前,内存中没有列表。
我想在执行数据库查询之前进行排序。我想在执行之前“编写”SQL查询。
我的代码:
public virtual DataTablesData<TViewModel> DataTablesGetData(DataTablesParam model)
{
var paged = new DataTablesData<TViewModel>();
IQueryable<TEntity> qr;
try
{
using (var db = new EdmxMSSQLContainer())
{
List<TViewModel> list = null;
qr = db.Set<TEntity>();
int iColumn = model.Order.FirstOrDefault().Column;
var property = typeof(TEntity).GetProperty(model.Columns.ToArray()[iColumn].Data);
var param = Expression.Parameter(typeof(TEntity));
Expression final = Expression.Property(param, property);
if (property.PropertyType.IsValueType)
{
final = Expression.MakeUnary(ExpressionType.Convert, final, typeof(object));
}
var lambda = Expression.Lambda<Func<TEntity, object>>(final, param);
if (model.Order.FirstOrDefault().Dir.Equals("asc"))
{
qr = qr.OrderBy(lambda);
}
else
{
qr = qr.OrderByDescending(lambda);
}
// THIS LINE THROW EXCEPTION
// qr.ToList() execute SQL Query
list = Mapper.Map(qr.ToList(), list);
paged.recordsTotal = this.CountRecords();
paged.recordsFiltered = list.Count();
paged.data = list;
}
}
catch (Exception ex)
{
OnError(ex);
}
return paged;
}
此行抛出异常:
list = Mapper.Map(qr.ToList(), list);
例外:
无法将类型System.Int32强制转换为System.Object类型。 LINQ to Entities仅支持转换EDM原语或枚举类型
答案 0 :(得分:0)
我找到了一个更好的方法来做到这一点。我必须这样做:
1 - 在项目中添加“Linq Dynamic”软件包:
Install-Package System.Linq.Dynamic.Library
2 - 在Class中导入包:
using System.Linq.Dynamic;
3 - 可通过属性的字符串名称查询的顺序:
qr.OrderBy(stringPropertyName); //asc
qr.OrderBy(stringPropertyName + " descending"); //des
它非常适合我。
答案 1 :(得分:0)
Func<IQueryable<Entity>, IOrderedQueryable<Entity>> orderBy = query => query.OrderBy(e => e.ID);
Expression<Func<Entity, bool>> fitler = e => e.Active;
public virtual IEnumerable<T> Get(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null)
{
IQueryable<T> query = dbSet;
if (filter != null)
query = query.Where(filter);
if (orderBy != null)
return orderBy(query).ToList();
else
return query.ToList();
}