为什么我不能在实现 IEnumerable 的类上使用 LINQ 方法?
我必须完成一个项目,但我有一个简单的问题
,我有一个以这种方式定义的类
using System;
using System.Collections;
using System.Collections.Generic;
..
public class GroupId: XmlDataTransferObject, IEnumerable
{
private IList _groupId;
private string _nameId;
public GroupId() : this("GroupId", "Id")
{
}
public GroupId(string rootName, string nomeId) : base(rootName)
{
_groupId = new ArrayList();
_nomeId = nomeId;
}
public override bool IsNull
{
get { return _groupId.Count == 0; }
}
public int Count
{
get { return _groupId.Count; }
}
public int Add(Intero i)
{
return _groupId.Add(i);
}
public Intero this[int index]
{
get { return (Intero)_groupId[index]; }
set { _groupId[index] = value; }
}
...
public IEnumerator GetEnumerator()
{
return _groupId.GetEnumerator();
}
}
}
,我需要找到两个 GroupId 对象的实例之间的交集。
即使我声明了以下语句,为什么我在可用方法中看不到 Linq Intersect:
Using System.Linq
...
var x = _groupId1.Intersect(_groupId2);
...
错误 1 '...GroupId' 不包含 'Intersect' 的定义,并且找不到接受类型为 '...GroupId' 的第一个参数的扩展方法 'Intersect' (您是否缺少 using 指令或程序集参考?)
I have to complete a project but I have a simple problem
I have a class defined in this way
using System;
using System.Collections;
using System.Collections.Generic;
..
public class GroupId: XmlDataTransferObject, IEnumerable
{
private IList _groupId;
private string _nameId;
public GroupId() : this("GroupId", "Id")
{
}
public GroupId(string rootName, string nomeId) : base(rootName)
{
_groupId = new ArrayList();
_nomeId = nomeId;
}
public override bool IsNull
{
get { return _groupId.Count == 0; }
}
public int Count
{
get { return _groupId.Count; }
}
public int Add(Intero i)
{
return _groupId.Add(i);
}
public Intero this[int index]
{
get { return (Intero)_groupId[index]; }
set { _groupId[index] = value; }
}
...
public IEnumerator GetEnumerator()
{
return _groupId.GetEnumerator();
}
}
}
and I need to find the intersection between an instance of two GroupId objects.
Why can't I see in the available methods the Linq Intersect even if I have declared the statement:
Using System.Linq
...
var x = _groupId1.Intersect(_groupId2);
...
Error 1 '....GroupId' does not contain a definition for 'Intersect' and no extension method 'Intersect' accepting a first argument of type '...GroupId' could be found (are you missing a using directive or an assembly reference?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
GroupId
类仅实现非泛型IEnumerable
类 - 如果您想使用 LINQ 扩展方法,则应实现IEnumerable
。 (无论如何,这通常会是更好的体验。)请注意,如果您使用通用
IList
而不是非通用IList
,这也会更容易 -如果可能的话,基本上尝试在新代码中完全避免非泛型集合。您可以使用
Cast
将IEnumerable
转换为IEnumerable
,如下所示:...但它会让您的类实现
IEnumerable
会更好。Your
GroupId
class only implements the non-genericIEnumerable
class - you should implementIEnumerable<T>
if you want to use the LINQ extension methods. (It'll generally be a better experience anyway.)Note that that will also be easier if you use a generic
IList<T>
instead of the non-genericIList
- basically try to avoid the non-generic collections entirely in new code, if at all possible.You could use
Cast
to convert yourIEnumerable
toIEnumerable<T>
like this:... but it would be much nicer to just make your class implement
IEnumerable<Intero>
.因为您的
_groupId1
被声明为IList
,而它只是一个IEnumerable
。几乎所有 linq 扩展方法都需要一个通用枚举 (
IEnumerable
)。您的选择是更改声明,或使用Cast
或OfType
扩展之一。尝试
_groupId1.OfType().Intersect(...)
或将其声明为
IList
Because your
_groupId1
is declared as anIList
, which is only anIEnumerable
.You need a generic enumerable (
IEnumerable<T>
) for nearly all the linq extension methods. Your choice is change the declaration, or use one of theCast<T>
orOfType<T>
extensions.try
_groupId1.OfType<Intero>().Intersect(...)
or declaring it as
IList<Intero>