Does linq-to-sql contain or not?

I am creating a polling widget. I have 2 tables, name them "Surveys and Polls". I need to make a linq query to get all the polls that do not exist for this user in PollCompleted.

I have the following sets:

Polls Where Active == True

For polls Where UserId == ThisUserId Where PollId = Polls .Id

Now I need to get all the polls that do not exist in PollCompleted. I need an example for this using one or more queries. I tried to break it into 2 questions.

Basically, I have 2 IQueryables of type T and T1. I want to take all T where T.ID does not exist in T1.ParentId.

+6
linq linq-to-sql
source share
2 answers
T.Where(x => ! T1.Select(y => y.ParentID).Contains(x.ID)) 

In Linq, you often work from the bottom up. Here we first get a collection of all the source identifiers in T1 - part of T1.Select (...). Then we create a where clause that selects all Ts whose identifiers are not contained in this set.

Please note that the result is a query. To materialize it, use ToList () or the like in the above description.

+15
source share

Use Except . This will work in this case.

For reference, Enumerable.Except Method

+6
source share

All Articles