Is it possible to compare with a number in "Where"?

48 Views Asked by At

I am new in Entity Framework and I have a little question. Is it possible to compare in "Where" with a number. I will add code example.

var source = db.Book.Where(book => book.Count > 0);

So, I need to get all book items where count more than zero. Thanks in advance for your help! Here is the structure of database

1

There are 1 best solutions below

0
Reza Heidari On

I'm not sure why you are trying to query the entire table to check if there is any record, yet you can do something like:

var query = context.Books.Any() ? context.Books.ToList() : null;

if (query is not null)
{
    query.ForEach(b =>
    {
        Console.WriteLine($"ID:{b.Id}, Title:{b.Title}");
    });
}

However, if you are trying to find out if there is duplication available, you can use the following query:

var query = context.Books.FirstOrDefault(b => b.Title == "some title") 
    is not null ? context.Books.ToList() : null;

if (query is not null)
{
    query.ForEach(b =>
    {
        Console.WriteLine($"ID:{b.Id}, Title:{b.Title}");
    });
}

By the way, I don't think it is a good idea to have such a scenario.