2013-03-19 39 views
1

在linq中編寫這個查詢的最佳方式是什麼?linq中的子查詢

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

回答

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(); 
+0

感謝奏效 – ChampChris 2013-03-19 00:59:41

+0

歡迎您':D' – 2013-03-19 01:02:29

0

JW的答案是正確的。另外,考慮到子查詢不相關,你可以單獨表達出來:

var pids = product.Where(p => p.buyer_user_id == 12); 
var r = product_master.Where(pm => pids.Contains(pm.product_id)); 
+0

感謝您的建議 – ChampChris 2013-03-19 01:00:14