为什么我不能在实现 IEnumerable 的类上使用 LINQ 方法?

发布于 2024-12-27 13:14:02 字数 1320 浏览 3 评论 0原文

我必须完成一个项目,但我有一个简单的问题

,我有一个以这种方式定义的类

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┈┾☆殇 2025-01-03 13:14:02

您的 GroupId 类仅实现非泛型 IEnumerable 类 - 如果您想使用 LINQ 扩展方法,则应实现 IEnumerable。 (无论如何,这通常会是更好的体验。)

请注意,如果您使用通用 IList 而不是非通用 IList,这也会更容易 -如果可能的话,基本上尝试在新代码中完全避免非泛型集合。

可以使用CastIEnumerable转换为IEnumerable,如下所示:

var x = _groupId1.Cast<Intero>().Intersect(_groupId2.Cast<Intero>());

...但它会让您的类实现 IEnumerable 会更好。

Your GroupId class only implements the non-generic IEnumerable class - you should implement IEnumerable<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-generic IList - basically try to avoid the non-generic collections entirely in new code, if at all possible.

You could use Cast to convert your IEnumerable to IEnumerable<T> like this:

var x = _groupId1.Cast<Intero>().Intersect(_groupId2.Cast<Intero>());

... but it would be much nicer to just make your class implement IEnumerable<Intero>.

扶醉桌前 2025-01-03 13:14:02

因为您的 _groupId1 被声明为 IList,而它只是一个 IEnumerable

几乎所有 linq 扩展方法都需要一个通用枚举 (IEnumerable)。您的选择是更改声明,或使用 CastOfType 扩展之一。

尝试 _groupId1.OfType().Intersect(...)

或将其声明为 IList

Because your _groupId1 is declared as an IList, which is only an IEnumerable.

You need a generic enumerable (IEnumerable<T>) for nearly all the linq extension methods. Your choice is change the declaration, or use one of the Cast<T> or OfType<T> extensions.

try _groupId1.OfType<Intero>().Intersect(...)

or declaring it as IList<Intero>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文