我的Enumerable类不适用于像c#中的.where这样的Linq语句

nev*_*hil 5 c# linq ienumerable object

我希望能够使用Linq的'.where'语句和我的类'Books'(一个'Book'列表)来实现接口IEnumerable.

//THE PROBLEM IS HERE.
IEnumerable list3 = bookList.Where(n => n.author.Length >= 14);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

错误1'Assignment2CuttingPhilip.Books'不包含'Where'的定义,也没有扩展方法'Where'可以找到接受'Assignment2CuttingPhilip.Books'类型的第一个参数(你是否缺少using指令或汇编引用?) C:\ Users\Alex\Dropbox\cos570 CSharp\Assignment2CuttingPhilip\Assignment2CuttingPhilip\Assignement2PhilipCutting.cs 132 33 Assignment2CuttingPhilip

我的守则如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Assignment2CuttingPhilip
{
    public class Book 
    {
        public string title;
        public string author;

        public Book(string title, string author) {
            this.title = title;
            this.author = author;
        }

        public override string  ToString()
        {  return "Title:\'" + title + "\', Author:" + author;  }

    }

    public class Books : IEnumerable
    {
        private Book[] _books;
        public Books(Book[] bArray){
            _books = new Book[bArray.Length];
            for (int i = 0; i < bArray.Length; i++)
            {_books[i] = bArray[i];}
        }

        IEnumerator IEnumerable.GetEnumerator()
        {return (IEnumerator) GetEnumerator();}

        public BooksEnum GetEnumerator()
        {return new BooksEnum(_books);}

    }

    public class BooksEnum : IEnumerator{
        public Book[] _books;
        int position = -1;

        public BooksEnum(Book[] list){
            _books = list;}

        public bool MoveNext()
        {
            position++;
            return (position < _books.Length);
        }

        public void Reset()
        {position = -1;}

        object IEnumerator.Current
        { get { return Current; }}

        public Book Current
        {{ try
                { return _books[position];}
                catch (IndexOutOfRangeException)
                {throw new InvalidOperationException();}
        }}
    }

    class Assignement2PhilipCutting
    {
        static void Main(string[] args)
        {
            Book[] bookArray = new Book[3] 
            {
                new Book("Advance C# super stars",  "Prof Suad Alagic"),
                new Book("Finding the right lint",  "Philip Cutting"),
                new Book("Cat in the hat",          "Dr Sues")
            };

            Books bookList = new Books(bookArray);

            IEnumerable List = from Book abook in bookList  
                               where abook.author.Length <= 14
                               select abook;

            IEnumerable list2 = bookArray.Where(n => n.author.Length >= 14);

            //**THE PROBLEM IS HERE**.
            IEnumerable list3 = bookList.Where(n => n.author.Length >= 14);

            foreach (Book abook in List)
            { Console.WriteLine(abook); }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这应该是非常直接的,但为什么我不能在C#中使用Linq的Enumerable Books列表?我不应该能够创建一个可以使用Fluent Linq命令查询的可枚举列表吗?

谢谢菲尔

Cro*_*ono 9

你的Books班级必须实施IEnumerable<Book>,而不仅仅是IEnumerable.该Where扩展,因为大多数LINQ的扩展,是实施对象进行的IEnumerable<T>.此接口位于System.Collections.Generic命名空间中.

现在你可以使用Cast()扩展名:

var list3 = bookList.Cast<Book>().Where(n => n.author.Length >= 14);
Run Code Online (Sandbox Code Playgroud)

这是您可以对仅实现的旧集合执行的操作IEnumerable.但是,在您的场景中,Books该类是您的,所以我真的建议您实现它IEnumerable<Book>.


Mat*_*zer 5

您需要实现IEnumerable<T>(通用的)而不仅仅是IEnumerable,并且您将能够使用与LINQ相关的扩展方法.