0

I am just starting with ASP.NET MVC Core.

Everything goes well until now, and when I want to parse some data to Book model it look like that can not recognize an Any method and FirstOrDefault.

My code is below. Any help ?

public string GetDeweyIndex(int id)
{
    if (_context.Books.Any(book => book.Id == id))
    {
        return _context.Books
                       .FirstOrDefault(book => book.id == id).DeweyIndex;
    }
    else 
        return "";
}

Book Model

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace LibaryData.Models
{
    public class Book : LibaryAsset
    {
        [Required]
        public string ISBN { get; set; }

        [Required]
        public string Author { get; set; }

        [Required]
        public string DeweyIndex { get; set; }

    }
}

LibaryContent

public class LibaryContext : DbContext    {

        public LibaryContext(DbContextOptions options) : base(options) { }

        public DbSet<Patron> Patrons { get; set; }
        public DbSet<Video> Videos { get; set; }
        public DbSet<Checkout> Checkouts { get; set; }
        public DbSet<CheckoutHistory> CheckOutHistories { get; set; }
        public DbSet<LibaryBranch> LibaryBrances { get; set; }
        public DbSet<BranchHours> BranchHours { get; set; }
        public DbSet<LibaryCard> LibaryCards { get; set; }
        public DbSet<Status> Statuses { get; set; }
        public DbSet<LibaryAsset> LibaryAssets { get; set; }
        public DbSet<Hold> Holds { get; set; }
        public bool Books { get; set; }
    }
}

I get this kind of error

Error CS1061
'bool' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?)
LibaryServices
C:\Users\Demir\Documents\Visual Studio 2017\Projects\Libary-Managment-System\LibaryServices\LibaryAssetsService.cs

Error CS1061
'bool' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?) LibaryServices
C:\Users\Demir\Documents\Visual Studio 2017\Projects\Libary-Managment-System\LibaryServices\LibaryAssetsService.cs 57 Active

5
  • Can you include the definition of Books on the _context object? Apr 14, 2018 at 8:49
  • The error suggests that what you're calling .Any / .FirstOrDefault on, _context.Books, is a bool. It shouldn't be, it should be something like a DbSet<Book>. Can you show how it is defined?
    – user743382
    Apr 14, 2018 at 8:50
  • Of course, I possted book.cs model
    – user9644896
    Apr 14, 2018 at 8:53
  • public bool Books { get; set; } -- Yeah, that explains the error. Is this a typo of some sort or did you really want something other than DbSet<Book> here?
    – user743382
    Apr 14, 2018 at 8:57
  • thank you @hvd I didnt see this stupid error in my model. Now works :)
    – user9644896
    Apr 14, 2018 at 9:02

1 Answer 1

2

To move from the comments to an answer:

The error message is saying

'bool' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?)

Emphasis mine. This makes sense, a bool is just true or false, it does not hold any items.

You've shown the definition of Books:

public bool Books { get; set; }

This should have type DbSet<Book>, similar to how all the other properties in your context are defined.

(I'm going to make a guess: I suspect you started with DbSet<, started typing boo, had your IDE auto-complete it to bool rather than Book, got an error because DbSet<bool> isn't allowed, and fixed that error the wrong way. Even if this guess is incorrect in your case, I've seen similar things happen before, so it may serve as a hint for others who do make that mistake and can't figure out why they wrote what they did.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.