gpt4 book ai didi

c# - 我的 Enumerable 类不适用于 C# 中的 .where 等 Linq 语句

转载 作者:太空狗 更新时间:2023-10-29 23:58:54 29 4
gpt4 key购买 nike

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

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

我得到以下错误:

Error 1 'Assignment2CuttingPhilip.Books' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'Assignment2CuttingPhilip.Books' could be found (are you missing a using directive or an assembly reference?) 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# 中将我的 Enumerable Books 列表与 Linq 一起使用?难道我不能制作一个可以使用 Fluent Linq 命令查询的可枚举列表吗?

谢谢菲尔

最佳答案

你的 Books类必须实现 IEnumerable<Book> ,不只是 IEnumerable . Where与大多数 LINQ 扩展一样,扩展是为实现 IEnumerable<T> 的对象创建的.此接口(interface)位于 System.Collections.Generic命名空间。

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

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

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

关于c# - 我的 Enumerable 类不适用于 C# 中的 .where 等 Linq 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22898380/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com