【发布时间】:2013-05-01 06:36:46
【问题描述】:
我在 asp.net mvc 应用程序中使用带有 edmx 文件和 POCO 的实体框架 4。
首先我有一个人类,它映射到数据库中的一个表。
public class Person
{
public Int32 ID{get;set;}
public string Name{get;set;}
public Int32? ParentID{get;set;}
}
然后在我的服务层中,我有以下功能来检索所有人。如果提供了 parentID,则检索到的人将是具有该 parentID 的人:
public List<Person> Get(int? parentPersonID = null)
{
var persons = Repository().GetAll(c => c.ParentID == parentPersonID);
}
最后,Repository() 函数返回一个IRepository<Person>,其中包含方法:
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;
}
现在的问题是,如果我将 null 作为 parentPersonID 传递给服务层,例如 Get(null)。枚举不会产生任何结果。但是,如果我将服务层代码修改为:
public List<Person> Get(int? parentPersonID = null)
{
var persons = Repository().GetAll(null);
}
一切都按预期进行。
有什么想法吗?
编辑: 如果我将服务层功能代码替换为:
var persons = Repository().GetAll(c => c.ParentID.Equals(parentPersonID));
代替:
var persons = Repository().GetAll(c => c.ParentID == parentPersonID);
它按预期工作 - 第一行从数据库中检索记录,而第二行没有。
我仍然很好奇 Equals() 和 == 在这种情况下有什么区别。
【问题讨论】:
标签: c# .net entity-framework-4 linq-to-entities