0

Hi I'm development a Web API in C# with entity framework, I have 4 tables in my database

Products: 
    IdProduct, 
    IdCategory, 
    IdBrand, 
    Product, 
    Price, 
    Descryption, 
    IdOffice

Category: 
    IdCategory, 
    Category

Brand: 
    IdBrand, 
    Brand

Office: 
    IdOffice, 
    Office

And I did a Web API with the Products model but I want to show the data and not the Id.

This is what I have done but gives me an error in the return

Cannot implicitly convert type 'System.Linq.IQueryable' anonymous type int IdProducto, string Category, string Product, string Brand, string office, string Description, decimal? Price' in System.Linq.IQueryable NameProject

private ExamenPOO1Entities db = new ExamenPOO1Entities();
private ExamenPOO1Entities1 ca = new ExamenPOO1Entities1();
private ExamenPOO1Entities2 ma = new ExamenPOO1Entities2();
private ExamenPOO1Entities3 su = new ExamenPOO1Entities3();

// GET: api/Productoes
public IQueryable<Producto> GetProductos()
{
    var query =
        from p in db.Productos
        join c in ca.Categorias
        on p.IdCategoria equals c.IdCategoria
        join m in ma.Marcas 
        on p.IdMarca equals m.IdMarca
        join s in su.Sucursales
        on p.IdSucursal equals s.IdSucursal
        select new 
        { IdProducto =p.IdProducto, Categoria = c.Categoria1, Producto = p.Producto1,
            Marca = m.Marca1, Sucursal = s.Sucursal, Descripcion = p.Descripcion,
            Precio = p.Precio};
        return query;
}
6
  • Please post the error message. Commented Jun 1, 2018 at 14:49
  • 2
    Unrelated, but ouch about 4 db contexts - this means joins are done in memory. Are you sure you cant reduce these? Commented Jun 1, 2018 at 14:50
  • Ugh, joining across contexts is nasty for sure. Maybe it's that giving you the errors? We don't know unless you tell us what you error is... Commented Jun 1, 2018 at 14:51
  • After new keyword, type Producto. Commented Jun 1, 2018 at 14:53
  • Cannot implicitly convert type 'System.Linq.IQueryable' <<anonymous type int IdProducto, string Category, string Product, string Brand, string office, string Description, decimal? Price>>' in System.Linq.IQueryable <NameProject> Commented Jun 1, 2018 at 15:03

1 Answer 1

2

You are having this error because as you specified, your method must return a IQueryable<Producto> but instead you're returning an anonymous object when you use the code below

select new {
     IdProducto =p.IdProducto, 
     Categoria = c.Categoria1, 
     Producto = p.Producto1,
     Marca = m.Marca1, 
     Sucursal = s.Sucursal, 
     Descripcion = p.Descripcion,
     Precio = p.Precio
}

So in order to be consistent with the method's signature the select should create a new Producto

select new Producto
    { ... }
Sign up to request clarification or add additional context in comments.

3 Comments

But that won't be work either, at least not how the classes are defined right now. For example, the new object received a property Category/Categoria. The Product/Producto class does not have such a property. You may just want to return an IQueryable<object>.
So should do only 1 model with all my tables?
It'll work if you respect the 'Producto' properties and not putting all the things between curly braces, if you want all the data specified in your first select you should whether create a model with these props or return a IQueriable<object> and it'll work (Don't forget to change the signature in this case from IQueriable<Producto> to IQueriable<object>).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.