Error: Cannot convert Implicitly

Asked

Viewed 512 times

3

I’m getting the following error.

It is not possible to implicitly convert "System.Linch.Iqueryable" to "Testemercos.Models.Applicationproduct". There is an explicit conversion ?

in that part of the code:

public ApplicationProduct GetProductsIdAsync(long products)
    {
        try
        {
            var result = _context.Produtos.Where(x => x.Id == products);

            return result;
        }
        catch (Exception)
        {
            throw;
        }

    }
  • Just do me a favor, take that one off try-ctach, he’s only harming his code and doing nothing useful.

1 answer

4


I believe your variable long products be the identifier of the product you want to find. When you perform the filter by method Where(), you will get a collection of data, be it a IQueryable or IEnumerable.

To fetch the record by the identifier try using the method FirstOrDefault(), where you pass your condition and will return you the first record that meets your condition, if it exists. If it does not find will return you null:

public ApplicationProduct GetProductsIdAsync(long id)
{
    return _context.Produtos.FirstOrDefault(x => x.Id == id);
}
  • 2

    You could have taken that try-ctach, He’s just undermining his code and doing nothing useful. People do it because they read somewhere like that and think it’s right. In an answer the staff will do the same and will justify that it is because you taught so, even if it is only because the question did wrong. The reason people do wrong is not that they do not know, is that there are too many people teaching wrong. We need to take responsibility when we pass information on the internet because it will be consumed by many people.

  • It really makes sense. I will pay more attention to these points. Constructive criticism, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.