简体   繁体   English

带有2个表的linq简单查询

[英]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 我正在尝试从2个表中获得一个简单的联接查询,它们没有关系,到目前为止,我已收到此代码,但是有一个错误,我没有得到,请帮助

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 };

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. 如果其他所有方法均失败,请删除var关键字并从那里开始工作。

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 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM