gpt4 book ai didi

c# - IEnumerable Concat Missing,不包含 'Concat' 的定义

转载 作者:太空狗 更新时间:2023-10-29 21:38:39 28 4
gpt4 key购买 nike

我有以下继承 IEnumerable 的类

public class LinesEnumerable : IEnumerable<Point>
{
protected readonly IPointSeries _pointSeries;
protected readonly ICoordinateCalculator<double> _xCoordinateCalculator;
protected readonly ICoordinateCalculator<double> _yCoordinateCalculator;
protected readonly bool _isDigitalLine;

/// <summary>
/// Initializes a new instance of the <see cref="LinesEnumerable" /> class.
/// </summary>
/// <param name="pointSeries">The point series.</param>
/// <param name="xCoordinateCalculator">The x coordinate calculator.</param>
/// <param name="yCoordinateCalculator">The y coordinate calculator.</param>
/// <param name="isDigitalLine">if set to <c>true</c> return a digital line .</param>
public LinesEnumerable(IPointSeries pointSeries, ICoordinateCalculator<double> xCoordinateCalculator, ICoordinateCalculator<double> yCoordinateCalculator, bool isDigitalLine)
{
_pointSeries = pointSeries;
_xCoordinateCalculator = xCoordinateCalculator;
_yCoordinateCalculator = yCoordinateCalculator;
_isDigitalLine = isDigitalLine;
}

/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
/// </returns>
public virtual IEnumerator<Point> GetEnumerator()
{
return _isDigitalLine ?
(IEnumerator<Point>)new DigitalLinesIterator(_pointSeries, _xCoordinateCalculator, _yCoordinateCalculator) :
new LinesIterator(_pointSeries, _xCoordinateCalculator, _yCoordinateCalculator);
}

/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through the collection.
/// </returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

但是,当我尝试执行以下操作时:

linesEnumerable = linesEnumerable.Concat(new[] { new Point(viewportWidth, lastYCoordinate) });

它说“System.Collections.IEnumerable”不包含“Concat”的定义和最佳扩展方法重载“System.Linq.Queryable.Concat(System.Linq.IQueryable, System.Collections.Generic.IEnumerable)” ' 有一些无效参数

我已经添加了 System.Linq 命名空间

有人知道为什么会这样吗?

最佳答案

当两个集合的T类型不同时,编译器会给出这个错误。例如:

List<int> l1 = new List<int>();
List<string> l2 = new List<string>();

var l3 = l1.Concat(l2);
var l4 = l1.Union(l2);

ConcatUnion 调用将分别导致以下编译时错误:

'List' does not contain a definition for 'Concat' and the best extension method overload 'Queryable.Concat(IQueryable, IEnumerable)' requires a receiver of type 'IQueryable'

'List' does not contain a definition for 'Union' and the best extension method overload 'Queryable.Union(IQueryable, IEnumerable)' requires a receiver of type 'IQueryable'

这是令人困惑的,因为消息具有误导性并且 VS 智能感知识别扩展方法。解决方案是l1l2必须是相同类型T的列表。

关于c# - IEnumerable Concat Missing,不包含 'Concat' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33460858/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com