小编jef*_*han的帖子

在EF Code First Orderby Function期间,无法将类型'System.Int32'强制转换为'System.Object'

我在EF Code First中使用规范模式.当我通过操作进行排序时,VS会抛出一个新的异常

在此输入图像描述

规范模式是从eShopOnWeb复制的

我只是改变了一点,这是我的更改代码:

public class Specification<T> : ISpecification<T>
{ 

    public Expression<Func<T, object>> OrderBy { get; private set; }

    public Specification(Expression<Func<T, bool>> criteria)
    {
        Criteria = criteria;
    }

    public Specification<T> OrderByFunc(Expression<Func<T, object>> orderByExpression)
    {
        OrderBy = orderByExpression;
        return this;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的调用代码,它非常简单:

static void TestSpec()
    {
        var spec = new Specification<ExcelData>(x => x.RowIndex == 5)
            .OrderByFunc(x => x.ColumnIndex);

        using (var dbContext = new TechDbContext())
        {
            var top10Data = dbContext.ExcelData.Take(10).ToList();
            var listExcel = dbContext.ApplySpecification(spec).ToList();

            Console.WriteLine();
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果我评论 …

entity-framework casting specifications sql-order-by

6
推荐指数
1
解决办法
163
查看次数