簡體   English   中英

我的Enumerable類不適用於像c#中的.where這樣的Linq語句

[英]My Enumerable class does not work with Linq statements like .where in c#

我希望能夠使用Linq的'.where'語句和我的類'Books'(一個'Book'列表)來實現接口IEnumerable。

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

我收到以下錯誤:

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

這應該是非常直接的,但為什么我不能在C#中使用Linq的Enumerable Books列表? 我不應該能夠創建一個可以使用Fluent Linq命令查詢的可枚舉列表嗎?

謝謝菲爾

您的Books類必須實現IEnumerable<Book> ,而不僅僅是IEnumerable 作為大多數LINQ擴展, Where擴展是為實現IEnumerable<T>對象而創建的。 此接口位於System.Collections.Generic命名空間中。

現在你可以使用Cast()擴展:

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

對於僅實現IEnumerable舊集合,您可以執行此操作。 但是,在你的場景中, Books類是你的,所以我真的建議你讓它實現IEnumerable<Book>

您需要實現IEnumerable<T> (通用的)而不僅僅是IEnumerable ,並且您將能夠使用與LINQ相關的擴展方法。

您應該實現IEnumerable<T>而不僅僅是IEnumerable或調用Cast<Book> ,如下所示

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

我建議使用第一種方法,因為你有能力但是當你不能改變列表的實現時,你可以使用Cast<T>從非泛型可枚舉轉換為通用可枚舉。 但是要注意Cast<T>只會轉換不轉換的事實,因此任何自定義轉換運算符都會導致異常

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM