无法将类型“ System.Collections.Generic.IEnumerable <book>”隐式转换为“ int”

时间:2019-04-24 19:57:00

标签: c# linq compiler-errors

我有以下由Class类对象组成的班级和藏书。我必须编写一个linq查询,选择要写在1900年到2000年之间的书

class Book
{
    public string Title { set; get; }
    public int Year { set; get; }
} 

var v =  from book in books
         where book.Year > 1900 && book.Year < 2000
         select book;

但是我遇到了编译器错误:

error CS0029: cannot implicitly convert type "System.Collections.Generic.IEnumerable<Book>" to "int"

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

包含所有书籍的清单

List<Book> books = new List<Book> {...}

您想要的病例数

int counts = books.Count(p => p.Year > 1900 && p.Year< 2000);

代码没有检查,但是我认为它应该可以工作。