views:

1174

answers:

1

I've got two un-related (no FK's defined) tables. The first table contains some tasks for which a user may not have access. I need to find all those tasks - in this case, the joined table would contain nulls. How do I get them?

Here's the setup:

TimeData table 
- userID 
- taskID 
- hours


ApprovedTasks table (the one that should contain nulls)
- taskID 
- userID

The SQL query would look like this:

select * from TimeData td 
left join ApprovedTasks at
 on at.taskID = td.taskID and at.userID = td.userID
where at.taskID is null

Any way to pull that off using a LINQ to Entity query?

TIA

+2  A: 

Check out... http://stackoverflow.com/questions/801757/disjoint-union-in-linq

This should work...

var approvedTaks = from at in ApprovedTasks.Except(
                                        from at2 in ApprovedTasks
                                        where at2.userID == userId and at2.taskID==taskId
                                        select at2)
                    where at.userID == userId and at.taskID==taskId
                    select at;

but sorry don't have the database handy to test it.

David McEwing
Thanks David, I'll go give this a shot. At first glance though, I'm betting the Entity framework hasn't implemented "Except"... we shall soon see.
Chu
Ah... but you can still get the two lists and then do an Except on them using a List or other construct running the linq over them. Just cause you are using Linq to Entities doesn't mean that you always have to use it. I'm guessing they have though as they have implemented the related ANY.
David McEwing
At the risk of sounding contentious... wouldn't one want to avoid doing two lists and then doing the Except? That would mean you are pulling the data out of the db server and doing the work using .net - I would guess that to make it optimized, you'd want to keep all the load on the db server.
Chu
You are right. But I don't think you will need to go to that extreme.
David McEwing