无法在第三方课程上调用ToArray()

时间:2013-10-28 16:52:52

标签: c# linq

我打算在第三方课程(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();
        }
   }

此时我收到错误

  

错误5'CMS.SettingsProvider.InfoDataSet'   不包含'ToArray'的定义,也没有扩展方法   'ToArray'接受第一个类型的参数   'CMS.SettingsProvider.InfoDataSet'   可以找到(你错过了使用指令或程序集   引用?)

CategoryInfoProvider.GetChildCategories定义为:

public static InfoDataSet<CategoryInfo> GetChildCategories(int categoryId, string where, string orderBy, int topN, string columns, int siteId);

InfoDataSet定义为:

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();
}

看起来接口是正确实现的,我已经为LINQ包含了命名空间,我可以像List<int> i; i.ToArray();那样进行调用我错过了哪些拼图?

1 个答案:

答案 0 :(得分:1)

试着打电话给AsEnumerable()

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.AsEnumerable().ToArray();
        }
   }
}

问题似乎是编译器无法将children解析为IEnumerable,即使该集合显式实现了该接口。强制将集合视为IEnumerable,AsEnumerable应该允许ToArray正确解析。