There are tons of Q&A on stackoverflow related to my question , but I cannot deduce the reasoning of the issue and the solution that works best in this scenario;
So I've a method that allows you to pass a parentID, and based on the value records will be filtered using a LINQ query. The field in the database allows NULL values. Now If I compare fields using ==
operator in the where clause, the sql emitted is wrong (it doesn't uses IS NULL
for the comparison), and hence query yields 0 results. I solved this using Object.Equals()
method. That worked, but now I get an exception on passing a NON NULL value, an integer
Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.
So I wrote a simple method
using (TestEntities context = new Entities())
{
return from c in context.ItemMappings
where c.ParentID.Equals(parentID)
select new ItemDTO
{
ItemID = c.Item.ItemID,
ItemName = c.Item.ItemName,
ItemType = new ItemTypeDTO
{
TypeID = c.Item.Type.TypeID,
TypeName =c.Item.Type.TypeName
};
}