简体   繁体   English

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

[英]How to Cast Expression Func<T,int> to Func<T,object>

Im using EntityFramework Code First Generic Repository. 我正在使用EntityFramework Code First Generic Repository。 I have one filter method. 我有一个过滤方法。 Also this method make paging and sorting. 此方法也进行分页和排序。 Method like below 方法如下

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();

}

Type of sorting is Expression<Func<TEntity, object>> If i pass this parameter as Expression<Func<TEntity, object>> getting an exception invalid casting between int to object but Expression<Func<TEntity, string>> dosent throw any exception.. 排序类型是Expression<Func<TEntity, object>>如果我将此参数作为Expression<Func<TEntity, object>>传递Expression<Func<TEntity, object>>在int与object之间获得异常无效的转换但是Expression<Func<TEntity, string>> dosent throw any例外..

Any idea Thanks 任何想法谢谢

Type of sorting is Expression<Func<TEntity, object>> If i pass this parameter as Expression<Func<TEntity, object>> getting an exception invalid casting between int to object but Expression<Func<TEntity, string>> dosent throw any exception.. 排序类型是Expression<Func<TEntity, object>>如果我将此参数作为Expression<Func<TEntity, object>>传递Expression<Func<TEntity, object>>在int与object之间获得异常无效的转换但是Expression<Func<TEntity, string>> dosent throw any例外..

That's because int is a value type, while string is a reference type. 那是因为int是值类型,而string是引用类型。 An int needs to be boxed to be converted to object , and the Linq Expression API doesn't do it automatically. 需要将int装箱以转换为object ,而Linq Expression API不会自动执行此操作。 When you generate the expression, if you return an int , you need to add an Expression.Convert(<expr>, typeof(object)) around the expression before you return it. 生成表达式时,如果返回int ,则需要在返回表达式之前在表达式周围添加Expression.Convert(<expr>, typeof(object))

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM