我最近一直处于前端编程的世界,但我正在为我正在进行的项目重新投入C#。我正在定义一些模型和通用功能,并收到以下错误:
错误3参数2:无法从
'System.Collections.Generic.IEnumerable<T>
[c:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.5 \ mscorlib.dll]'转换为'System.Collections.Generic.IEnumerable<T>
[c:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.5 \ mscorlib.dll]'
错误来自以下函数中的return语句:
public static PagedResult<T> Create<T, TEntity>(IOrderedQueryable<TEntity> query, PagedRequest pagedRequest, Func<TEntity, T> converter)
where T : class
{
var count = query.Count();
if (pagedRequest.SortInfo.Fields.Any())
{
var firstPass = true;
foreach (var sortOrder in pagedRequest.SortInfo.Fields)
{
if (firstPass)
{
firstPass = false;
query = sortOrder.Direction == SortDirection.Ascending
? query.OrderBy(sortOrder.Field) :
query.OrderByDescending(sortOrder.Field);
}
else
{
query = sortOrder.Direction == SortDirection.Ascending
? query.ThenBy(sortOrder.Field) :
query.ThenByDescending(sortOrder.Field);
}
}
}
query = (IOrderedQueryable<TEntity>) query.Skip((pagedRequest.Page - 1) * pagedRequest.PageSize).Take(pagedRequest.PageSize);
var list = new List<T>();
foreach (var entity in query)
{
list.Add(converter(entity));
}
return Create(pagedRequest, list.AsEnumerable(), count);
}
和创建功能:
static public PagedResult<T> Create(PagedRequest request, IEnumerable<T> data, long totalCount)
{
var result = new PagedResult<T> {Status = ResultStatus.Successful, Data = data.ToArray()};
result.Count = result.Data.Count();
result.TotalCount = totalCount;
result.Page.Index = request.Page;
result.Page.Size = request.PageSize;
if (result.Page.Size > 0)
{ result.Page.Count = (long)Math.Ceiling((double)totalCount / result.Page.Size); }
return result;
}
似乎无法让编译时错误消失。有人对如何解决这个问题有任何想法吗?
答案 0 :(得分:0)
感谢@JeffMercado,解决方案非常简单。
我的第二个Create函数的方法签名必须是:
static public PagedResult<T> Create<T>(PagedRequest request, IEnumerable<T> data, long totalCount)
而不是:
static public PagedResult<T> Create(PagedRequest request, IEnumerable<T> data, long totalCount)