如何将表达式Func <t,int>转换为Func <t,object> </t,object> </t,int>

时间:2014-08-01 08:09:31

标签: c# entity-framework expression

我正在使用EntityFramework Code First Generic Repository。我有一个过滤方法。此方法也进行分页和排序。方法如下

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>>传递,则在int与object之间进行异常无效转换,但Expression<Func<TEntity, string>>剂量会抛出任何异常..

任何想法 感谢

2 个答案:

答案 0 :(得分:6)

  

排序类型为Expression<Func<TEntity, object>>如果我将此参数作为Expression<Func<TEntity, object>>传递,则在int与object之间进行异常无效转换,但Expression<Func<TEntity, string>>剂量会抛出任何异常..

那是因为int是值类型,而string是引用类型。需要将int装箱以转换为object,并且Linq Expression API不会自动执行此操作。生成表达式时,如果返回int,则需要在返回表达式之前添加Expression.Convert(<expr>, typeof(object))

答案 1 :(得分:-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;
}