【问题标题】:My Enumerable class does not work with Linq statements like .where in c#我的 Enumerable 类不适用于 Linq 语句,例如 c# 中的 .where
【发布时间】:2014-04-06 18:49:45
【问题描述】:

我希望能够将 Linq 的“.where”语句与实现接口 IEnumerable 的类“Books”(“Book”列表)一起使用。

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

我收到以下错误:

错误 1“Assignment2CuttingPhilip.Books”不包含“Where”的定义,并且找不到接受“Assignment2CuttingPhilip.Books”类型的第一个参数的扩展方法“Where”(您是否缺少 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 一起使用?难道我不能创建一个可以使用 Fluent Linq 命令查询的可枚举列表吗?

谢谢 菲尔

【问题讨论】:

    标签: c# linq object ienumerable


    【解决方案1】:

    您的Books 类必须实现IEnumerable&lt;Book&gt;,而不仅仅是IEnumerable。与大多数 LINQ 扩展一样,Where 扩展是为实现 IEnumerable&lt;T&gt; 的对象而设计的。此接口位于System.Collections.Generic 命名空间中。

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

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

    这是您可以对仅实现 IEnumerable 的旧集合执行的操作。但是,在您的场景中,Books 类是您的,所以我真的建议您实现 IEnumerable&lt;Book&gt;

    【讨论】:

    • 我自己怎么没有意识到这一点。感谢您指出这一点和很好的答案。
    【解决方案2】:

    您需要实现IEnumerable&lt;T&gt;(通用之一)而不仅仅是IEnumerable,您将能够使用与LINQ相关的扩展方法。

    【讨论】:

      【解决方案3】:

      您应该实现IEnumerable&lt;T&gt; 而不仅仅是IEnumerable 或调用Cast&lt;Book&gt;,如下所示

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

      我建议使用第一种方法,因为您有能力但是当您无法更改列表的实现时,您可以使用Cast&lt;T&gt; 将非通用可枚举转换为通用可枚举。但请注意Cast&lt;T&gt; 只会强制转换不会转换,因此任何自定义强制转换运算符都会导致异常

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多