I am using the entity framework 4 with edmx files and POCOs within an asp.net mvc application.
First of all I have a person class which is mapped to a table in the database.
public class Person
{
public Int32 ID{get;set;}
public string Name{get;set;}
public Int32? ParentID{get;set;}
}
Then in my service layer I have the following function to retrieve all persons. If a parentID is supplied the persons retrieved will be the ones with that parentID:
public List<Person> Get(int? parentPersonID = null)
{
var persons = Repository().GetAll(c => c.ParentID == parentPersonID);
}
Finally, the Repository() function returns an IRepository<Person>
which contains the method:
public IQueryable<TModel> GetAll(Expression<Func<TModel, bool>> predicate = null)
{
var result = ObjectSet.AsQuaryable(); //ObjectSet is a ObjectSet<Person> instance
if (predicate != null)
result = result.Where(predicate);
return result;
}
Now, the problem is that if I pass null as parentPersonID to the service layer, so as Get(null)
. Enumeration yields no results. However if I modify the service layer code to:
public List<Person> Get(int? parentPersonID = null)
{
var persons = Repository().GetAll(null);
}
everything works as expected.
Any ideas why is that?
EDIT: If i replace the service layer function code with:
var persons = Repository().GetAll(c => c.ParentID.Equals(parentPersonID));
instead of:
var persons = Repository().GetAll(c => c.ParentID == parentPersonID);
it works as expected - the first line retrieves records from the DB whereas the second one does not.
I am still curious as to what is the difference in the Equals()
and ==
in this case.