簡體   English   中英

linq中的子查詢

[英]Sub-Query in linq

在linq中編寫此查詢的最佳方法是什么?

SELECT *
  FROM product_master
  where product_id in (select product_id from product where buyer_user_id=12)
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();

JW的答案是對的。 或者,如果子查詢不相關,則可以單獨表達:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM