My NewsItem entity has a nullable foreign key: LibraryID type int? .
My problem is when I request a property and compare it with any value except null , I get exceptions.
Originally my code:
int? lid = ... var results = context.NewsItems .Where(n => n.LibraryID == lid);
but it gives me no results no matter what lid .
So I tried:
var results = context.NewsItems .Where(n => n.LibraryID.Equals(lid));
gives an exception:
Unable to create a constant value of type "System.Object". In this context, only primitive or enumeration types are supported.
and then I tried:
var results = context.NewsItems .Where(n => lid.Equals(n.LibraryID));
and received:
Cannot enter type "System.Nullable`1" to enter "System.Object". LINQ to Entities only supports EDM listing of primitive or enumerated types.
and this:
var results = context.NewsItems .Where(n => object.Equals(lid, n.LibraryID));
gives the same exception as the last.
Now I was desperate, so I tried to complicate the stuff (like other forums like here ):
var results = context.NewsItems .Where(n => (lid == null ? n.LibraryID == null : n.LibraryID == lid));
but still getting the same exception.
So ... any EASY workarounds?
source share