我已经编写了以下扩展方法:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Cortana.Extensions
{
public static class LinqExtensions
{
/// <summary>
/// Linq method to paginate data.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="pageSize">Size of the page.</param>
/// <returns></returns>
public static List<IEnumerable<T>> ToPages<T>(this IEnumerable<T> source, int pageSize)
{
List<IEnumerable<T>> pagedSource = source
.Select((x, index) => new { x, index })
.GroupBy(a => a.index / pageSize)
.Select(x => x.Select(i => i.x))
.ToList();
return pagedSource;
}
}
}
被这样称呼:
using Cortana.Extensions;
...
var pagedAssessments = Model.SymptomAssessmentHistory
.Where(x => x.IsComplete())
.Where(x => (x.SymptomAssessmentUID != Model.CurrentSymptomAssessment.SymptomAssessmentUID))
.OrderByDescending(x => x.TimeTaken)
.Take(numColumnsToShow)
.ToPages(numColumnsToShow);
但我得到以下编译器错误:
'System.Collections.Generic.IEnumerable<Cortana.Models.WebApi.SymptomAssessment>' does not contain a definition for 'ToPages' and no extension method 'ToPages' accepting a first argument of type 'System.Collections.Generic.IEnumerable<Cortana.Models.WebApi.SymptomAssessment>' could be found (are you missing a using directive or an assembly reference?)
一切似乎都很好,我错过了什么?
原来有人在我们的构建早期破坏了一些东西。无论如何,感谢大家的帮助。
如果这对其他人有帮助,请删除您的代码,并确保您可以在没有它的情况下进行构建…