Reputation: 37
This error is coming in my code.... 'ICollection' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'ICollection' could be found (are you missing a using directive or an assembly reference?)
// 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);
}
I have one more class name BookViewModel.cs having a function 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; }
}
}
Upvotes: 0
Views: 4311
Reputation: 791
I think you need to edit your book class to something like that:
public ICollection<YourSubClass> BorrowHistories { get; set; }
instead of:
public ICollection BorrowHistories { get; set; }
Because LinQ need a type to work correctly. I not see your BorrowHistories model, so i can't say if is enought.
Upvotes: 1