views:

32

answers:

2

Okay I'm new to EF and I'm having issues grasping on filtering results...

I'd like to emulate the ef code to do something like:

select * 
from order o  
    inner join orderdetail d on (o.orderid = d.orderid)
where d.amount > 20.00

just not sure how this would be done in EF (linq to entities syntax)

A: 

I would do it like that:

context.OrderDetails.Where(od => od.Amount > 20).Include("Order").ToList().Select(od => od.Order).Distinct();

We are taking details first, include orders, and take distinct orders.

LukLed
That gives different results than his SQL, but I think his SQL is buggy, so it's hard to tell.
Craig Stuntz
That gives the same result. It return only details with amount > 20 and adds their orders.
LukLed
+1  A: 

Your SQL gives multiple results per order if there's multiple details > 20.00. That seems wrong to me. I think you want:

var q = from o in Context.Orders
        where o.OrderDetails.Any(d => d.Amount > 20.00)
        select o;
Craig Stuntz