AlexGH
AlexGH

Reputation: 2805

Linq subquery same table using lambda

I've been trying for a while to use Linq and lambda expressions to translate a query that already exists in SQL, but there is something I'm missing...

This is the SQL query:

select o.ord_no from orders o  where 1 <= 
(select count(*) from orders where orders.purch_amt < o.purch_amt 
and orders.ord_date = '2012-02-14')

How could I do the same query but using Linq and lambda expressions???

Upvotes: 2

Views: 1649

Answers (1)

Mohammad Akbari
Mohammad Akbari

Reputation: 4766

try this:

var date = DateTime.ParseExact("20120214", 
                              "yyyyMMdd", 
                               CultureInfo.InvariantCulture);    

var result = dbContext.orders
        .Where(q => dbContext.orders
                     .Where(s => s.purch_amt < q.purch_amt)
                     .Where(s => s.ord_date == date).Count() > 0)
        .ToList()

Upvotes: 4

Related Questions