代码之家  ›  专栏  ›  技术社区  ›  Iain Fraser

初学者:我的ilist.where()方法去哪儿了?

  •  3
  • Iain Fraser  · 技术社区  · 16 年前

    我还有一个简单的(我想)让我很困惑。我在一个控件中编写了一个方法,该方法在给定文件名的CMS中获取文件的最新版本(即,无论文件位于哪个文件夹中)。我发现它非常有用,我以为我会把它扔到我的cmstoolbox类中,但是当我这样做的时候,我不能再使用 Where() CMS提供的FileManager类的方法(返回列表)。

    下面是我的课程的一个简化示例:

    using System;
    using System.Collections.Generic;
    using CMS.CMS;
    using CMS.Core;
    using CMS.Web;
    
    namespace CoA.CMS {
        public class ToolBox
        {
            public CMS.CMS.File getLatestFileVersionByFilename(string filename, int GroupID)
            {
                IList<CMS.CMS.File> fileWithName = FileManager.GetGroupAll(false, GroupID).Where(file => currentFileVersionIsNamed(file, filename)).ToList<CMS.CMS.File>();
                return getLatestFileFromListOfFiles(fileWithName);
    
            }
            protected bool currentFileVersionIsNamed(CMS.CMS.File file, string name)
            {
            }
            protected CMS.CMS.File  getLatestFileFromListOfFiles(CMS.CMS.File file)
            {
            }
        }
    }
    

    当我在控件的上下文中执行完全相同的操作时(实际上是CMS提供的类,它扩展了 Control )我可以访问 其中()) 方法,但在我的工具箱类中,我没有。有什么好处?我觉得 IList 总是允许从您使用它的任何地方访问相同的方法。

    我又错了,哈哈:)


    编辑: Filemanager.GetGroupAll() 返回A CMSList 延伸 伊利斯特

    3 回复  |  直到 16 年前
        1
  •  12
  •   Joel Coehoorn    16 年前

    您需要的使用指令 System.Linq . .Where() 是上的扩展方法 IEnumerable<T> (其中 IList<T> 实现)在System.Linq命名空间中定义的。

        2
  •  3
  •   Robert Fraser    16 年前

    Joel是第一个,但为了扩展它:where()是 extension method . 扩展方法是类似于实际方法的静态方法,声明如下:

    static class NameNeverUsed
    {
        public static void AnExtensionMethod(this string x)
        {
        }
    }
    

    像这样叫:

    "hello".AnExtensionMethod();
    

    它们需要通过使用其他语句来导入。因此,与Java/C++不同,一个类可以有声明在它之外的方法。

        3
  •  0
  •   Nate CSS Guy    16 年前

    iirc,that.where方法是linq的一部分,您需要在类中添加那些using语句来获取IEnumerable接口的扩展方法。