English 中文(简体)
C# Beginner: Where has my IList.Where() method gone?
原标题:

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

最佳回答

You need a using directive for System.Linq. .Where() is an extension method on IEnumerable<T> (which IList<T> implements) that is defined in the System.Linq namespace.

问题回答

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:

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

And called like:

"hello".AnExtensionMethod();

They need to imported by using statements like anything else. So unlike Java/C++ a class can have methods declared outside it.

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签