Help me please. How can I filter a collection of objects when filtering parameters can be more than 10 and need to check them for the contents of values ​​(all parameters are nullable)? Tried with ICriteria chain checking and adding Restrictions. But I do not like magic strings and in one case I need to get access to the internal field of the class. For example, the method

public string(int? firstParam, int? secondParam, int? thirdParam, int? fourthParam, int? fiveParam, int? sixthParam, int? seventhParam, DateTime? time) { var session = DbSession.JoinOrOpen(); var collection = session.Linq<ObjectWithManySettings>()... // Либо через лямбда методы collection = from item in session.Linq<ObjectWithManySettins> } 

    1 answer 1

    It is not clear why such difficulties.

    If you need to query the database table by filtering it by some columns, this is easily done using lambda expressions.

    Nhibernate like all ORM supports building queries through the IQueryable<T> interface.

    Those. if the DB entity has the following form:

     public class SomeEntity { public int Price {get; set;} public string Name {get; set;} public bool IsDeleted {get; set;} } 

    Then using the Nhibernate Session class and the Query<T> method

    The request might look like this:

     session.Query<SomeEntity>().Where(x=> x.Price = 3 && x.IsDeleted).Select(x=>x.Name); 

    to search for all entity names with a price = 3 and a delete flag.

    You can also build table joins and use SQL-like syntax to build queries.

    If you are confused by the number of arguments that are often repeated, then you can make a specification, for example using LinqSpecs (it’s better to do your implementation just by keeping the Expression)

     public class SomeSpec: Specification<SomeEntity> { private int count; private bool isDeleted; public SomeSpec(int count, bool isDeleted) { this.count= count; this.isDeleted = isDeleted; } public override Expression<Func<SomeEntity, bool>> IsSatisfiedBy() { return x => x.Price == price && x.IsDeleted == isDeleted; } } 

    And use the specification already in filtering, type

     session.Query<SomeEntity>() .Where(new SomeSpec(3, true).IsSatisfied()) .Select(x=>x.Name); 
    • Not quite that =)) You can of course create an entity, but the fact is that we do not have default values, so the variables are nullable On criteria, it is easy to build long conditions for checking and remove unnecessary ones or not filter at all if all values! Value.HasValue It seems to me that LINQ cannot build a query that simultaneously checks the values ​​for non-null and only after that turn it on in the filter. Yesterday the whole evening I was tormented and it was impossible to implement anything - Sam-Fisher