Ben*_*Ben 2 .net c# linq nhibernate linq-to-nhibernate
我想为我提供以下API MyTypeRepository:
var myChosenInstance = _myRepository.FindOne(x => x.MyProperty == "MyValue");
Run Code Online (Sandbox Code Playgroud)
..以及用于在存储库中构造linq查询的lambda,然后由Linq用于NHibernate.
这可能吗?我的存储库FindOne方法会是什么样的?
public EntityType FindOne<EntityType>(Expression<Func<EntityType,bool>> predicate)
{
return session.Linq<EntityType>().FirstOrDefault(predicate);
}
Run Code Online (Sandbox Code Playgroud)
我在假设
ISession名为的变量sessionFirstOrDefault()方法(因为我没有测试它来确认)如果您的存储库类具有类型参数Repository<EntityType>,则可以省略该方法中的type参数.