Null Reference Exception In LINQ DataContext

Posted by Frank on Stack Overflow See other posts from Stack Overflow or by Frank
Published on 2009-08-20T20:52:36Z Indexed on 2010/06/06 7:02 UTC
Read the original article Hit count: 415

Filed under:
|
|
|
|

I have a Null Reference Exception Caused by this code:

var recentOrderers = (from p in db.CMS
            where p.ODR_DATE > DateTime.Today - new TimeSpan(60, 0, 0, 0)
            select p.SOLDNUM).Distinct();
result = (from p in db.CMS
             where p.ORDER_ST2 == "SH" &&
                   p.ODR_DATE > DateTime.Today - new TimeSpan(365, 0, 0, 0) &&
                   p.ODR_DATE < DateTime.Today - new TimeSpan(60, 0, 0, 0) &&
                   !(recentOrderers.Contains(p.SOLDNUM))/**/
             select p.SOLDNUM).Distinct().Count();

result is of double type. When I comment out:

!(recentOrderers.Contains(p.SOLDNUM))

The code runs fine. I have verified that recentOrderers is not null, and when I run:

if(recentOrderes.Contains(0)) return;

Execution follows this path and returns. Not sure what is going on, since I use similar code above it:

var m = (from p in db.CMS where p.ORDER_ST2 == "SH" select p.SOLDNUM).Distinct();
            double result = (from p in db.CUST
                        join r in db.DEMGRAPH on p.CUSTNUM equals r.CUSTNUM
                        where p.CTYPE3 == "cmh" && !(m.Contains(p.CUSTNUM)) &&
                              r.ColNEWMEMBERDAT.Value.Year > 1900
                        select p.CUSTNUM).Distinct().Count();

which also runs flawlessly. After noting the similarity, can anyone help? Thanks in advance.

-Frank GTP, Inc.

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ