C# 初学者:我的 IList.Where() 方法去哪儿了?
我还有另一个简单的问题(我认为)困扰着我。我在我的一个控件中编写了一种方法,可以根据文件名获取 CMS 中文件的最新版本(即,无论文件驻留在哪个文件夹中)。我发现它非常有用,我想我应该将它放入我的 CMSToolbox 类中,但是当我这样做时,我不能再使用 CMS 提供的 FileManager 类的 Where()
方法(该方法返回一个列表)。
这是我的类的一个简化示例:
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)
{
}
}
}
当我在 Control 的上下文中执行完全相同的操作(实际上是 CMS 提供的扩展 Control
的类)时,我可以访问 Where( )
方法,但在我的 ToolBox 类中我没有。什么给?我认为 IList
总是允许从您使用它的任何地方访问相同的方法。
我又错了,哈哈:)
编辑:Filemanager.GetGroupAll()
返回一个CMSList
,它扩展了IList
I've got another simple one (I think) that's stumping me. I have written a method in one of my controls that gets the latest version of a file in a CMS given it's filename (i.e. regardless of what folder the file resides in). I found it useful enough that I thought I'd chuck it in my CMSToolbox class, but when I do this I can no longer use the Where()
method of a FileManager class provided by the CMS (which returns a list).
Here's a simplified example of my class:
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)
{
}
}
}
When I do exactly the same thing in the context of a Control (really a class provided by the CMS which extends Control
) I have access to the Where()
method, but in my ToolBox class I don't. What gives? I figured that an IList
would always allow access to the same methods from wherever you use it.
I'm a wrong again, haha :)
Edit: Filemanager.GetGroupAll()
returns a CMSList
which extends IList
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要 System.Linq 的 using 指令。
.Where()
是IEnumerable
(IList
实现)的扩展方法,在 System.Linq 中定义命名空间。You need a using directive for
System.Linq
..Where()
is an extension method onIEnumerable<T>
(whichIList<T>
implements) that is defined in the System.Linq namespace.Joel 是第一个,但要对此进行扩展:Where() 是一个扩展方法< /a>.扩展方法是静态方法,其行为类似于真实方法,并且声明如下:
调用方式如下:
它们需要像其他方法一样通过 using 语句导入。因此,与 Java/C++ 不同,类可以在其外部声明方法。
Joel was first, but to expand on that: Where() is an extension method. Extension methods are static methods that act like real methods, and are declared like this:
And called like:
They need to imported by using statements like anything else. So unlike Java/C++ a class can have methods declared outside it.
IIRC,.Where 方法是 LINQ 的一部分,您需要在类中添加这些 using 语句来获取 IEumerable 接口的扩展方法。
IIRC, that .Where method is part of LINQ, and you need to add those using statements in your class to get the extension methods for the IEumerable interface.