开发者

All items in list query with nHibernate

I know there is the Restriction "In" which allows to detect if one or more elements in property is in target list but I need something which would tell me if all the items from property are in target list. Here's an example:

I want a person with all these Competencies

CompetenciesCriterion : IList<Competency>
  { Walking, Running, Rolling }

Between PersonA and PersonB:

PersonA : Person
  Competencies : IList<Competency>
    { Walking }

PersonB : Person
  Competencies : IList<Competency>
    { Walking, Rolling, Running }

Is there a Restriction or Expression which would allow me to execute this search or do you know a clean way to do this开发者_运维技巧 instead of stacking the "In" within a "Conjunction"?

Thanks in advance,

Étienne Brouillard


I found a way to get the result I wanted. Stacking "In" is not working so I figured out that using a subquery and counting the "In" will produce a criteria on which I can verify the condition that the counts are equal.

Here's a sample:

var competencySubQuery = DetachedCriteria.For<Employee>("employee2");
competencySubQuery.CreateAlias("employee2.Competencies", "employee2Competencies");
competencySubQuery.SetProjection(Projections.Count(Projections.Property("employee2Competencies.Competency")));
competencySubQuery.Add(Restrictions.In("employee2Competencies.Competency",     searchCriteria.Competencies));
competencySubQuery.Add(Restrictions.EqProperty("employee2.Id", "Employee.Id"));
criteriaJunction.Add(Subqueries.Eq(searchCriteria.Competencies.Count(), competencySubQuery));

criteriaJunction being the criteria used in the end for the FindAll and searchCriteria being the POCO containing the criteria sent to a service.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜