如何强制转换表达式funct,int>Func< T, object>
本文关键字:Func object 何强制 int 表达式 funct 转换 | 更新日期: 2023-09-27 18:15:59
我使用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到对象的无效转换,但Expression<Func<TEntity, string>>
没有抛出任何异常…
任何想法由于
类型的排序是
Expression<Func<TEntity, object>>
如果我传递这个参数作为Expression<Func<TEntity, object>>
得到一个异常在int到对象之间无效的转换,但Expression<Func<TEntity, string>>
没有抛出任何异常..
这是因为int
是值类型,而string
是引用类型。int
需要装箱才能转换为object
,而Linq Expression
API不会自动执行此操作。在生成表达式时,如果返回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;
}