Estefano Salazar
Estefano Salazar

Reputation: 419

Find Entities with NHibernate from other Object

I have a really big problem: I am trying to find Entities (with NHibernate) using other object. Example: Emplyee (entity) -> { Name, LastName, Email, Address } EmplyeeSearchFilters (filters object) -> { Name, LastName, Email, Address } Then, I want to pass EmployeeSearchFilters to the Repository (generic) and automatically find with NHibernate using the properties that are not null. Example: EmployeeSearchFilters -> { Name = "Nelly", LastName = null, Email = "@fibertel.com", Addres = null } Someone have idea how can I do that?

Thanks!

Upvotes: 0

Views: 399

Answers (2)

Anas Jaber
Anas Jaber

Reputation: 593

You can use linq by use this function:

    public virtual List<T> RetrieveByList<T>(Expression<Func<T, bool>> predicate)
    {
        return Session.Query<T>().Where(predicate).ToList<T>();
    }

and can use like this:

    RetrieveByList<Customer>(x=>x.Name == "Nelly" && x.LastName == null && x.Email == "@fibertel.com" && x.Addres == null)

and you can use with dynamic predicate http://www.codeproject.com/Articles/28580/LINQ-and-Dynamic-Predicate-Construction-at-Runtime

Upvotes: 1

csanchez
csanchez

Reputation: 121

I think the NHibernate.Criterion.Example criterion can be used for that purpose. Doc can be found here

Upvotes: 2

Related Questions