缺少使用指令或程序集引用

时间:2018-07-16 12:25:11

标签: c# asp.net-mvc

此错误即将出现在我的代码中。... “ ICollection”不包含“任何”的定义 并且没有扩展方法“ Any”接受第一个参数 可以找到类型为“ ICollection”的(您是否缺少 使用指令还是程序集引用?)

      // my BooksController.cs Code
      public ActionResult Index()
      {
             var books = db.Books.Include(h => h.BorrowHistories)
             .Select(b => new BookViewModel
             {
                  BookId = b.BookId,
                  Author = b.Author,
                  Publisher = b.Publisher,
                  SerialNumber = b.SerialNumber,
                  Title = b.Title,
                  IsAvailable = !b.BorrowHistories.Any(h => h.ReturnDate == null)  //Error is coming in this line
              }).ToList();
        return View(books);
       }

我还有一个类名称BookViewModel.cs,它具有一个功能public bool IsAvailable。

        // my Book.cs
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.ComponentModel.DataAnnotations;
        using System.Collections;

        namespace LibraryManagmentSystem.Models
        {
         public class Book
         {
           public int BookId { get; set; }
           [Required]
           public string Title { get; set; }
           [Required]
           [Display(Name = "Serial Number")]
           public string SerialNumber { get; set; }
           public string Author { get; set; }
           public string Publisher { get; set; }
           public ICollection BorrowHistories { get; set; }

          }
         }

1 个答案:

答案 0 :(得分:1)

我认为您需要将您的书本课编辑为类似的内容:

public ICollection<YourSubClass> BorrowHistories { get; set; }

代替:

public ICollection BorrowHistories { get; set; }

因为LinQ需要一种类型才能正常工作。我看不到您的BorrowHistories模型,所以我不能说是否足够。

相关问题