我有以下继承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) });
它说&#39; System.Collections.IEnumerable&#39;不包含&#39; Concat&#39;的定义和最好的扩展方法重载&#39; System.Linq.Queryable.Concat(System.Linq.IQueryable,System.Collections.Generic.IEnumerable)&#39;有一些无效的论点
我已经添加了System.Linq命名空间
有人知道为什么会这样吗?
答案 0 :(得分:4)
这不会帮助您,但是当您搜索错误时,此问题是最重要的结果,所以我要说我的问题是什么。
我有以下课程:
class Group
class Row : Group
class Column : Group
我试图用IEnumerable<Row>
调用IEnumerable<Column>
上的Concat来获得IEnumerable<Group>
。这不起作用,因为您无法将列转换为行。解决方案是将我的IEnumerable<Row>
投射到IEnumerable<Group>
。
例如rows.Cast<Group>().Concat(columns)
答案 1 :(得分:1)
尝试添加以下命名空间:
using System.Collections;
using System.Collections.Generic;
答案 2 :(得分:0)
当两个集合具有不同类型T
时,编译器将给出此错误。例如:
List<int> l1 = new List<int>();
List<string> l2 = new List<string>();
var l3 = l1.Concat(l2);
var l4 = l1.Union(l2);
Concat
和Union
调用将分别导致以下编译时错误:
“列表”不包含“ Concat”的定义,最佳扩展方法重载“ Queryable.Concat(IQueryable,IEnumerable)”需要类型为“ IQueryable”的接收器
“列表”不包含“联盟”的定义,最佳扩展方法重载“ Queryable.Union(IQueryable,IEnumerable)”需要类型为“ IQueryable”的接收器
这令人困惑,因为该消息具有误导性,并且VS intellisense可以识别扩展方法。解决方案是l1
和l2
必须是相同类型T
的列表。