1

在 linq 中编写此查询的最佳方法是什么?

SELECT *
  FROM product_master
  where product_id in (select product_id from product where buyer_user_id=12)
4

2 回答 2

3
var _result =   from a in product_master
                where (product.Where(s => s.buyer_user_id == 12))
                        .Contains(a.product_ID)
                select a;

或者

var _result =   (from a in product_master
                join b in product
                    on a.product_id equals b.product_id
                where b.buyer_user_id == 12
                select a).Distinct();
于 2013-03-19T00:47:06.977 回答
0

JW 的回答是正确的。或者,鉴于子查询不相关,您可以单独表达它:

var pids = product.Where(p => p.buyer_user_id == 12);
var r = product_master.Where(pm => pids.Contains(pm.product_id));
于 2013-03-19T00:56:07.573 回答