Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8326393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:44:43+00:00 2026-06-09T00:44:43+00:00

In the project I am working on, we are passing a specification (ala the

  • 0

In the project I am working on, we are passing a specification (ala the Specification pattern as put forward by Martin Fowler) to a repository that uses NHibernate as its backend. The predicates to determine whether a given object are implemented as lambdas wrapped as Expressions, which the repository hands off to the Criteria API in NHibernate to actually fetch the result.

What we have looks something like:

class MySpecification : ISpecification<AnEntity>
{
      public Expression<Func<AnEntity, bool>> Satisfies
      {
           get { return entity => entity.Children.Count(c => !c.IsDeleted) == 0; }
      }
}

and in the repository:

public IEnumerable<AnEntity> Select(ISpecification<AnEntity> spec)
{
      ICriteria criteria = Session.CreateCriteria<AnEntity>();
      criteria.Add(NHibernate.Criterion.Expression.Where(spec.Satisfies));
      return criteria.List<AnEntity>();
}

At runtime, we get exceptions of the form:

{variable 'entity' of type 'AnEntity' referenced from scope '', but it is not defined"} System.Exception {System.InvalidOperationException}

When we reduce the predicates to simple boolean expressions that perform no set operations (e.g. Any, Count, Where) on child collections, everything works fine.

With that in mind, is there any way to build a criterion using a lambda expression that operates on the child collections of the main entity?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:44:44+00:00Added an answer on June 9, 2026 at 12:44 am

    It is not possible to do such things with Criteria API. You are trying to mix Linq and Criteria API. Criteria API has own methods to work with child collection.

    You could achieve what you want using linq-to-nhibernate. Try to change your method like following

    public IEnumerable<AnEntity> Select(ISpecification<AnEntity> spec)
    {
        return Session.Query<AnEntity>() 
            .Where(spec.Satisfies)
            .ToList();
    }
    

    Query is an extension method NHibernate.Linq.LinqExtensionMethods.Query(this ISession session)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project that uses Hibernate3 and JDBC to connect/interact with our
I've been working on a django project for a while now that uses grappelli
I was working on a project and had my Activities passing each other information
I am working on a project, and had all of my tests passing on
I'm working on a Haskell project that involves tying a big knot: I'm parsing
I'm working on a project that is trying to use context-free grammars for parsing
I have a working project that Im amending, it crashes after trying to use
I am working in a project that reads files and processes data. There I
I am working on an iOS project that loads UIImages from URLs in a
I have a setup project that I'm working with and have added a EULA

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.