Using LINQ Expression Instead of NHIbernate.Criterion

If I were to select some rows based on certain criteria, I could use an object ICriterionin NHibernate.Criterion, for example:

  public List<T> GetByCriteria()
  {
  SimpleExpression newJobCriterion =
    NHibernate.Criterion.Expression.Eq("LkpStatu", statusObject);
  ICriteria criteria = Session.GetISession().CreateCriteria(typeof(T)).SetMaxResults(maxResults);
  criteria.Add(newJobCriterion );
  return criteria.List<T>();
  }

Or I can use the LINQ where clause to filter what I want:

  public List<T> GetByCriteria_LINQ()
  {

  ICriteria criteria = Session.GetISession().CreateCriteria(typeof(T)).SetMaxResults(maxResults);

  return criteria.Where(item=>item.LkpStatu=statusObject).ToList();
  }

I would prefer the second, of course. Because

  • It gives me a strong typing
  • I don't need to learn another syntax like NHibernate

The problem is, is there a performance advantage in the first for the second? From what I know, the first one will create SQL queries, so it will filter the data before going into memory. How effective is such performance to justify its use?

+3
source share
2

, . , .List() return criteria. , . , maxResults, , , maxResults, .

, , , , . , , .

.

, NHibernate.Linq( Contrib project). linq .

+3

:

var crit = _session.CreateCriteria(typeof (T)).SetMaxResults(100);
return (from x in _session.Linq<T>(crit) where x.field == <something> select x).ToList();
+1

Source: https://habr.com/ru/post/1707020/


All Articles