C# 无法在第三方类上调用ToArray()

C# 无法在第三方类上调用ToArray(),c#,linq,C#,Linq,我曾计划在第三方类(Kentico)上使用一些LINQ,但似乎无法做到这一点,我不知道为什么。我的代码基本上是: using System; using System.Collections.Generic; using System.Linq; // some additional namespaces namespace test { public partial class ProductFilter : CMSAbstractBaseFilterControl {

我曾计划在第三方类(Kentico)上使用一些LINQ,但似乎无法做到这一点,我不知道为什么。我的代码基本上是:

using System;
using System.Collections.Generic;
using System.Linq;
// some additional namespaces

namespace test
{
   public partial class ProductFilter : CMSAbstractBaseFilterControl
   {
       protected IEnumerable<String> GetCategories(String parentName)
        {
            CategoryInfo info = CategoryInfoProvider.GetCategoryInfo(parentName, CMSContext.CurrentSite.SiteName);
            var children = CategoryInfoProvider.GetChildCategories(info.CategoryID, null, null, -1, null, CMSContext.CurrentSite.SiteID);

            children.ToArray();
        }
   }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
//一些附加名称空间
名称空间测试
{
公共部分类ProductFilter:CMSAbstractBaseFilterControl
{
受保护的IEnumerable GetCategories(字符串parentName)
{
CategoryInfo info=CategoryInfoProvider.GetCategoryInfo(parentName,CMSContext.CurrentSite.SiteName);
var children=CategoryInfoProvider.GetChildCategories(info.CategoryID,null,null,-1,null,CMSContext.CurrentSite.SiteID);
儿童。ToArray();
}
}
在这一点上,我得到了错误

错误5“CMS.SettingsProvider.InfoDataSet” 不包含“ToArray”的定义,并且没有扩展方法 “ToArray”接受类型为的第一个参数 “CMS.SettingsProvider.InfoDataSet” 无法找到(是否缺少using指令或程序集 参考?)

CategoryInfoProvider.GetChildCategories定义为:

public static InfoDataSet<CategoryInfo> GetChildCategories(int categoryId, string where, string orderBy, int topN, string columns, int siteId);
public class InfoDataSet<InfoType> : ObjectDataSet<BaseInfo>, IEnumerable<InfoType>, IInfoDataSet, IEnumerable where InfoType : CMS.SettingsProvider.BaseInfo, new()
{
    public InfoDataSet();
    public InfoDataSet(DataSet sourceData);

    public InfoObjectCollection<InfoType> Items { get; protected set; }
    protected InfoType Object { get; set; }

    public InfoDataSet<InfoType> Clone();
    public IEnumerator<InfoType> GetEnumerator();
    public InfoType GetNewObject(DataRow dr);
    protected override ObjectCollection<BaseInfo> NewCollection();
}
publicstaticinfodatasetgetchildcategories(int-categoryId、stringwhere、stringorderby、int-topN、stringcolumns、int-siteId);
InfoDataSet定义为:

public static InfoDataSet<CategoryInfo> GetChildCategories(int categoryId, string where, string orderBy, int topN, string columns, int siteId);
public class InfoDataSet<InfoType> : ObjectDataSet<BaseInfo>, IEnumerable<InfoType>, IInfoDataSet, IEnumerable where InfoType : CMS.SettingsProvider.BaseInfo, new()
{
    public InfoDataSet();
    public InfoDataSet(DataSet sourceData);

    public InfoObjectCollection<InfoType> Items { get; protected set; }
    protected InfoType Object { get; set; }

    public InfoDataSet<InfoType> Clone();
    public IEnumerator<InfoType> GetEnumerator();
    public InfoType GetNewObject(DataRow dr);
    protected override ObjectCollection<BaseInfo> NewCollection();
}
公共类InfoDataSet:ObjectDataSet,IEnumerable,IInfoDataSet,IEnumerable其中InfoType:CMS.setingsProvider.BaseInfo,new()
{
公共信息数据集();
公共信息数据集(数据集sourceData);
公共InfoObjectCollection项{get;protected set;}
受保护的信息类型对象{get;set;}
公共信息数据集克隆();
公共IEnumerator GetEnumerator();
公共信息类型GetNewObject(DataRow dr);
受保护的重写ObjectCollection NewCollection();
}

看起来接口的实现是正确的,我已经为LINQ提供了名称空间,我可以进行调用,比如
List I;I.ToArray();
我缺少的是谜题的哪一部分?

尝试调用
AsEnumerable()

使用系统;
使用System.Collections.Generic;
使用System.Linq;
//一些附加名称空间
名称空间测试
{
公共部分类ProductFilter:CMSAbstractBaseFilterControl
{
受保护的IEnumerable GetCategories(字符串parentName)
{
CategoryInfo info=CategoryInfoProvider.GetCategoryInfo(parentName,CMSContext.CurrentSite.SiteName);
var children=CategoryInfoProvider.GetChildCategories(info.CategoryID,null,null,-1,null,CMSContext.CurrentSite.SiteID);
children.AsEnumerable().ToArray();
}
}
}

问题似乎是编译器无法将
子项
解析为IEnumerable,即使集合显式地实现了接口。强制将集合视为IEnumerable(AsEnumerable实现了这一点),应该可以正确解析数组。

是否可以对每个子项进行解析?@FlorianDohrendorf:I can foreach yesTry
IEnumerable children
而不是
var children
…还可以尝试“转到定义/实现”在该
GetChildCategories
上,调用并查看它是否找到了您想要的内容。您的代码将无法编译,因为您没有返回语句。也许您可以发布可编译的示例?@nvoigt抱歉-只需取出返回。除非您安装Kentico(我不推荐),否则我无法为您提供兼容的代码。我永远不会得到返回因为我无法形成我的LINQ语句。我实际上尝试了一个numerable()它以完全相同的方式抱怨。您是否尝试显式定义收集的类型?如果ObjectDataSet还实现了泛型IEnumerable,则基础实现和派生实现之间可能存在歧义;
子项将同时是
IEnumerable
IEnumerable
,并且可能无法明确定义要使用的编译器。请尝试
AsEnumerable()
,看看会发生什么。如果这样做有效,请简单地尝试
ToArray()
。我已尝试根据Alexis的注释将var切换到IEnumerable,Alexis的注释确实进行了编译。