Linq to SQL Where In Clause Causes Object not set to instance of object

I have two tables, subscriptions and topics. Each subscription is related to a specific TopicID (PK for topics table). The first query works fine and retrieves the topicID's of all videos that were uploaded today.

The problem occurs when I try and then use the results of query1 as a where-in clause for query 2. I keep receiving object reference not set to instance of an object.

Query 1

IQueryable<int> topics = (from t in dataLayer.Videos
                  where SqlMethods.DateDiffDay(t.DateCreated, DateTime.Today) == 0
                 select t.TopicID).Distinct();

Query 2 (fails)

 IQueryable<Subscription> subs = from s in dataLayer.Subscriptions
                                     where topics.Contains(s.TopicID)
                                     select s;

The Linq query generated when it fails is {Table(Subscription).Where(s => value(EmailSubscribers+<>c__DisplayClass0).topics.Contains(s.TopicID))}

Any insight into this would be appreciated. I have looked at some samples around the net and they all seem to be identical to mine.

Answers


You might need to ToList() your first query so that it is definitely executed.


Need Your Help

How does NewRelic's `Availability monitoring` monitor CDN's website?

monitoring newrelic high-availability

I have a domain that is hosted by a CDN(Akamai). This means depending on visitor's geographic region this domain will resolve into different IPs closest to the user.