扩展方法。不包含定义,并且没有接受类型的第一个参数的扩展方法

问题描述 投票:0回答:2

我写了以下扩展方法:

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?)

一切似乎都已就位,我错过了什么?

c# linq extension-methods
2个回答
0
投票

事实证明,有人在我们构建的早期阶段破坏了某些东西。无论如何,感谢大家的帮助。

如果这对其他人有帮助 - 删除您的代码并确保您可以在没有它的情况下进行构建...


0
投票

我遇到了类似的问题,最终注意到扩展方法在另一个程序集中声明了

internal
。不得不改成
public

IDE 知道它在那里,甚至当我按下“转到定义”时会导航到它,但由于访问修饰符的原因,它对编译器不可见。

© www.soinside.com 2019 - 2024. All rights reserved.