My Enumerable class does not work with Linq operators like .where in C #

I want to be able to use the Linq '.where' operator with my Books class (Book list), which implements the IEnumerable interface.

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

And I get the following error:

Error 1 'Assignment2CuttingPhilip.Books' does not contain a definition for “Where” and the “Where” extension method is not used; the first argument of the “Assignment2CuttingPhilip.Books” type is accepted (do you miss the use directive or build link?) C: \ Users \ Alex \ Dropbox \ cos570 CSharp \ Assignment2CuttingPhilip \ Assignment2CuttingPhilip \ Assignement2PhilipCutting.cs 132 33 Assignment2CuttingPhilip

My code is as follows:

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

, Enumerable Books Linq #? , Fluent Linq?

+4
3

Books IEnumerable<Book>, IEnumerable. Where, LINQ, , IEnumerable<T>. System.Collections.Generic.

Cast():

var list3 = bookList.Cast<Book>().Where(n => n.author.Length >= 14);

, , IEnumerable. Books , IEnumerable<Book>.

+9

IEnumerable<T> (), IEnumerable, , LINQ.

+5

IEnumerable<T>, IEnumerable Cast<Book>,

IEnumerable list3 = bookList.Cast<Book>.Where(n => n.author.Length >= 14);

, , , Cast<T> . , Cast<T> ,

+4

Source: https://habr.com/ru/post/1535272/


All Articles