Problem using Linq to join tables in ASP.NET MVC
I used to have a straight up controller returning a view like this
return View(db.Stuff.ToList());
This works fine. I started laborating a bit and found that this:
var items = from g in db.Stuff
select g;
return View(items);
...also works fine. However, as I try to join with another table :
var items = from g in db.Stuff
join ug in db.OtherStuff on g.Id equals ug.StuffId
开发者_如何学运维 where !ug.UserId.Equals(1)
select g;
return View(items);
I get an error in the view saying:
Unable to create a constant value of type 'System.Object'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
Why is this? Another odd thing is that if I comment out the where clause, it seams to work again (work as in not throw an exception)
Try changing your where clause to this:
where ug.UserId != 1
精彩评论