如何将表达式 Func<T,int> 转换为 Func<T,object>?

5

我正在使用EntityFramework Code First通用仓储库。我有一个过滤器方法。此方法还可以进行分页和排序。方法如下所示:

public IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> filter, out int total, Expression<Func<TEntity, object>> sorting, SortType sortDirection, int index = 1, int size = 30)
{
    index = index - 1;

    int skipcount = index * size;
    IQueryable<TEntity> resetSet = filter != null ? Entities.Where(filter) : Entities.AsQueryable();

    total = Entities.Where(filter).Count();
    if (sortDirection == SortType.Desc)
    {
                resetSet = skipcount == 0 ?
                    resetSet.Where(filter).OrderByDescending(sorting).Skip(0).Take(size) :
                    resetSet.Where(filter).OrderByDescending(sorting).Skip(skipcount).Take(size);
    }
    else
    {
                resetSet = skipcount == 0 ?
                    resetSet.Where(filter).OrderBy(sorting).Skip(0).Take(size) :
                    resetSet.Where(filter).OrderBy(sorting).Skip(skipcount).Take(size);
    }

            return resetSet.AsQueryable();

}

排序类型是Expression<Func<TEntity, object>>。如果我将此参数作为Expression<Func<TEntity, object>>传递,则会出现无效的强制转换异常,但是Expression<Func<TEntity, string>>不会引发任何异常。

有什么想法 谢谢


“sorting” 是从哪里来的?你是手动使用 Expression API 生成它吗? - Thomas Levesque
顺便说一句,你在 skipcount == 0 的测试是多余的:使用 Skip(skipcount)Skip(0) 结果相同... - Thomas Levesque
2个回答

6
排序类型是 Expression<Func<TEntity, object>> 如果我把这个参数作为 Expression<Func<TEntity, object>> 传递,就会出现无效的转换异常,因为 int 类型是值类型,而 string 类型是引用类型。要将 int 转换为 object 必须装箱,而 Linq 的 Expression API 并不会自动执行此操作。当您生成表达式时,如果返回一个 int,则在返回之前需要在表达式周围添加 Expression.Convert(<expr>, typeof(object))

谢谢,但我该怎么做呢?你能给我分享一个例子吗?Expression<Func<TEntity, object>> sort = Expression.Convert(sorting, typeof(object)); 没有起作用。 - Yılmaz Buhar
@PolatYerimdar,这个表达式最初来自哪里? - Thomas Levesque
代码块已共享。 - Yılmaz Buhar
@PolatYerimdar,这是什么意思? - Thomas Levesque
在排序之前使用.Cast<object>() - Alexander Derck

-1
public virtual PagedList<Product> SelectPagedProductsByFilter(Expression<Func<Product, bool>> predicate, DataPaginationParameters paginationParameter, DataSortingParameters sorting)
{
            Expression<Func<Product, object>> sortExpression = null;
            SortType sortDirection = SortType.Asc;

            if (sorting.Sortby == 1) { sortExpression = x => x.Id; sortDirection = SortType.Desc; }
            if (sorting.Sortby == 2) { sortExpression = x => x.Name; sortDirection = SortType.Asc; }
            if (sorting.Sortby == 3) { sortExpression = x => x.Name; sortDirection = SortType.Desc; }
            if (sorting.Sortby == 4) { sortExpression = x => x.Price; sortDirection = SortType.Asc; }
            if (sorting.Sortby == 5) { sortExpression = x => x.Price; sortDirection = SortType.Desc; }
            if (sorting.Sortby == 6) { sortExpression = x => x.ProductDiscount; sortDirection = SortType.Asc; }
            if (sorting.Sortby == 7) { sortExpression = x => x.ProductDiscount; sortDirection = SortType.Desc; }


            int total = 0;
            var query = from p in _productRepository.Filter(predicate, out total, sortExpression, sortDirection,
                            paginationParameter.Page, paginationParameter.PageSize)
                        select new
                        {
                            Brand = p.Brand,
                            BrandId = p.BrandId,
                            ShortDescription = p.ShortDescription,
                            Price = p.Price,
                            ProductId = p.Id,
                            Name = p.Name,
                            ProductCode = p.ProductCode,
                            Barcode = p.Barcode,
                            SlugIdentifier = p.Page.SlugIdentifier,
                            Slug = p.Page.Slug
                        };

            var alisami = query.ToList();

            var products = query.ToList().Select(p => new Product
            {
                Brand = p.Brand,
                BrandId = p.BrandId,
                ShortDescription = p.ShortDescription,
                Price = p.Price,
                Id = p.ProductId,
                Name = p.Name,
                ProductCode = p.ProductCode,
                Barcode = p.Barcode,
                Page = new Page
                {
                    Slug = p.Slug,
                    SlugIdentifier = p.SlugIdentifier
                }
            });

            PagedList<Product> pagedList = new PagedList<Product>
            {
                Items = products.ToList(),
                CurrentPage = paginationParameter.Page,
                Total = total
            };

            return pagedList;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接