C#C
C#2y ago
SWEETPONY

✅ How to correctly write conditions with expressions?

I have this code:
public static class BookPolicy
    {
        public static void AllowRead(Book book)
        {
            var isHorrorBook = Specifications.IsHorrorBook().Compile();
            var isPonyBook = Specifications.IsPonyBook().Compile();

            if (isHorrorBook(book) || isPonyBook(book))
                throw new Exception("Horror or pony books don't allow here");
        }
    }

    public static class Specifications
    {
        public static Expression<Func<Book, bool>> IsHorrorBook() =>
            book => book.Title.Contains("Frankenstein");
        
        public static Expression<Func<Book, bool>> IsPonyBook() =>
            book => book.Title.Contains("Pony");
    }


everything is ok but i would like to know is it possible to write smth like this? if(book is Horror or Pony) {...}
I don't understand how to do this with expressions
Was this page helpful?