【问题标题】:Extension Method. Does not contain a definition for and no extension method accepting a first argument of type扩展方法。不包含接受类型的第一个参数的定义和扩展方法
【发布时间】:2014-05-24 05:06:01
【问题描述】:

我写了以下扩展方法:

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

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

【问题讨论】:

  • 您需要将类型传递给 ToPages 不是吗?
  • @Brandon 没有。第一个参数是一个 IEnumerable,所以 T 会被猜到。
  • @RaphaëlAlthaus 你能详细说明一下吗?我认为 IQueryable 扩展了 IEnumerable 所以这不重要。
  • @PaulCoghill 您是否在项目中添加了对Cortana.Extensions.dll 的引用?
  • @Selman22 是的,这个命名空间中的其他方法都可以,特别是这个方法。

标签: c# linq extension-methods


【解决方案1】:

原来有人在我们构建的早期破坏了某些东西。无论如何感谢大家的帮助。

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

【讨论】:

    猜你喜欢
    • 2016-04-13
    • 1970-01-01
    • 2023-03-13
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    • 2013-05-21
    • 1970-01-01
    相关资源
    最近更新 更多