I've been off in a world of front end programming lately, but am diving back into C# for a project I am working on. I am defining some of my models and generic functionality, and am getting the following error:
Error 3 Argument 2: cannot convert from
'System.Collections.Generic.IEnumerable<T>
[c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\mscorlib.dll]' to'System.Collections.Generic.IEnumerable<T>
[c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\mscorlib.dll]'
The error is coming from the return statement in the following function:
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);
}
and the Create function:
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;
}
Can't seem to get the compile time error to go away. Anybody have any ideas on how to fix this?
Thanks to @JeffMercado, the solution was very simple.
The method signature for my second Create function needed to be:
static public PagedResult<T> Create<T>(PagedRequest request, IEnumerable<T> data, long totalCount)
instead of:
static public PagedResult<T> Create(PagedRequest request, IEnumerable<T> data, long totalCount)