C# 如何强制转换表达式函数<;T、 int>;to Func<;T、 对象>;
Im使用EntityFramework代码作为第一个通用存储库。我有一个过滤方法。该方法还可以进行分页和排序。方法如下C# 如何强制转换表达式函数<;T、 int>;to Func<;T、 对象>;,c#,entity-framework,expression,C#,Entity Framework,Expression,Im使用EntityFramework代码作为第一个通用存储库。我有一个过滤方法。该方法还可以进行分页和排序。方法如下 public IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> filter, out int total, Expression<Func<TEntity, object>> sorting, SortType sortDirection, int
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();
}
公共IQueryable筛选器(表达式筛选器、out int total、表达式排序、SortType sortDirection、int index=1、int size=30)
{
指数=指数-1;
int skipcount=索引*大小;
IQueryable resetSet=filter!=null?Entities.Where(filter):Entities.AsQueryable();
总计=实体。其中(过滤器).Count();
if(sortDirection==SortType.Desc)
{
resetSet=skipcount==0?
resetSet.Where(筛选器).OrderByDescending(排序).跳过(0).获取(大小):
resetSet.Where(过滤器).OrderByDescending(排序).Skip(skipcount).Take(大小);
}
其他的
{
resetSet=skipcount==0?
resetSet.Where(筛选器).OrderBy(排序).跳过(0).获取(大小):
resetSet.Where(过滤器)、OrderBy(排序)、Skip(skipcount)、Take(大小);
}
返回resetSet.AsQueryable();
}
排序类型为Expression
,如果我将此参数作为Expression
传递,则在int到object之间获取异常无效强制转换,但Expression
dosent抛出任何异常
有什么想法吗
谢谢
排序类型为Expression
,如果我将此参数作为Expression
传递,则在int到object之间获取异常无效强制转换,但Expression
dosent抛出任何异常
这是因为int
是一种值类型,而string
是一种引用类型。int
需要装箱才能转换为对象
,而Linq表达式
API不会自动执行此操作。生成表达式时,如果返回一个int
,则需要在表达式周围添加一个表达式。在返回表达式之前,转换(,typeof(object))
。公共虚拟页面列表SelectPagedProductsByFilter(表达式谓词,数据分页参数分页参数,数据排序参数排序)
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;
}
{
表达式sortExpression=null;
SortType sortDirection=SortType.Asc;
如果(sorting.Sortby==1){sortExpression=x=>x.Id;sortDirection=SortType.Desc;}
if(sorting.Sortby==2){sortExpression=x=>x.Name;sortDirection=SortType.Asc;}
如果(sorting.Sortby==3){sortExpression=x=>x.Name;sortDirection=SortType.Desc;}
if(sorting.Sortby==4){sortExpression=x=>x.Price;sortDirection=SortType.Asc;}
如果(sorting.Sortby==5){sortExpression=x=>x.Price;sortDirection=SortType.Desc;}
如果(sorting.Sortby==6){sortExpression=x=>x.ProductDiscount;sortDirection=SortType.Asc;}
如果(sorting.Sortby==7){sortExpression=x=>x.ProductDiscount;sortDirection=SortType.Desc;}
int-total=0;
var query=来自p in_productRepository.Filter(谓词、out total、sortExpression、sortDirection、,
paginationParameter.Page,paginationParameter.PageSize)
选择新的
{
品牌=p.品牌,
BrandId=p.BrandId,
ShortDescription=p.ShortDescription,
价格=p.价格,
ProductId=p.Id,
名称=p.名称,
ProductCode=p.ProductCode,
条形码=p.条形码,
SlugIdentifier=p.Page.SlugIdentifier,
Slug=p.Page.Slug
};
var alisami=query.ToList();
var products=query.ToList().Select(p=>newproduct
{
品牌=p.品牌,
BrandId=p.BrandId,
ShortDescription=p.ShortDescription,
价格=p.价格,
Id=p.ProductId,
名称=p.名称,
ProductCode=p.ProductCode,
条形码=p.条形码,
页面=新页面
{
段塞=p.段塞,
SlugIdentifier=p.SlugIdentifier
}
});
PagedList PagedList=新建页面列表
{
Items=products.ToList(),
CurrentPage=paginationParameter.Page,
总计=总计
};
返回页面列表;
}
排序从哪里来?您是否使用表达式
API手动生成它?顺便说一句,您对skipcount==0
的测试是多余的:使用Skip(skipcount)
的结果与使用Skip(0)
的结果相同……谢谢,但我该怎么做呢。你能给我举个例子吗。表达式排序=Expression.Convert(排序,typeof(object));不起作用。@PolatYerimdar,表达式首先来自哪里?代码块是共享的。@PolatYerimdar,这是什么意思?或者在排序之前使用.Cast()