I use the following query to get a result set
var overlaps = from s in db.signups
join u in db.users on new { userid = s.userid } equals new { userid = u.studentid }
join a in db.activities on new { activityid = s.activityid } equals new { activityid = a.id }
where
s.userid != Convert.ToInt32(Request.Cookies["studentid"].Value) &&
(from signups in db.signups
where
signups.userid == Convert.ToInt32(Request.Cookies["studentid"].Value)
select new
{
signups.activityid
}).Contains(new { s.activityid })
orderby
u.studentid
select new
{
a.name,
u.firstname,
u.lastname,
u.studentid,
u.email
};
I’m pretty new to LINQ so I actually wrote the Sql and then used Linqer to generate the LINQ, so if this can be done more efficiently then please let me know. Having said that, this is not the problem.
The problem is that when I do
foreach(var overlap in overlaps)
{
//do something
}
it throws the object reference not set error. This is being run in an MVC 3 application.
However, when this is run in a Console application, it runs without issue; it just returns no results. I’ve tried using DefaultIfEmpty but just can’t find anything that addresses how to use this with anonymous types.
So
… is my approach correct?
If not, what should I do differently?
Thanks, in advance.
I don’t know if this is your problem, but your join syntax is really weird.
You don’t have to build anonymous types here, just compare directly.
Same with this:
Can be just:
And why in the world do you want to redo all the work to convert the cookie parameter to an int over and over?