ChampChris
ChampChris

Reputation: 1631

Sub-Query in linq

What is the best way to write this query in linq?

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

Upvotes: 1

Views: 122

Answers (2)

Douglas
Douglas

Reputation: 54887

JW's answer is correct. Alternatively, given that the subquery is not correlated, you could express it separately:

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

Upvotes: 0

John Woo
John Woo

Reputation: 263723

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

or

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

Upvotes: 3

Related Questions