在linq中编写此查询的最佳方法是什么?
SELECT *
FROM product_master
where product_id in (select product_id from product where buyer_user_id=12)
答案 0 :(得分: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();
答案 1 :(得分:0)
JW的回答是正确的。或者,如果子查询不相关,则可以单独表达:
var pids = product.Where(p => p.buyer_user_id == 12);
var r = product_master.Where(pm => pids.Contains(pm.product_id));