VictorC
VictorC

Reputation: 326

linq simple query with 2 tables

I am trying to get a simple join query from 2 tables , they have no relationship , so far i got this code but there is an error that i dont get it , please help

var query =
                    from p in db.Productos
                    from s in db.Stocks
                    where p.Nombre == "Suaje" && p.TipoProducto == tipo
                    where p.RecID == s.IDProducto
                    select new { s.Largo, s.Ancho };

Upvotes: 0

Views: 172

Answers (2)

Amy B
Amy B

Reputation: 110111

Your query is well formed. I believe the error comes from how you're using the query object.

Are you returning it? What is the return type of the method?

Good practice dictates that you do not return anonymous types (or generic types with anonymous type parameters) from any method.

If all else fails, remove the var keyword and work from there.

Upvotes: 1

p.campbell
p.campbell

Reputation: 100567

Are you missing the 'and'?

  and p.RecID == s.IDProducto

Consider writing your query like this:

var results = from p in db.Productos
join s in db.Stocks
on p.RecID equals s.IDProducto
where p.Nombre == "Suaje" && p.TipoProducto == tipo
select new { s.Largo, s.Ancho };

Upvotes: 0

Related Questions