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 7910643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:56:28+00:00 2026-06-03T12:56:28+00:00

For a project that I’m developing, I have to use Linq to create some

  • 0

For a project that I’m developing, I have to use Linq to create some specifications. Now I have a Dictionary on which I have to search the values. Since the default Linq implementation for NHibernate doesn’t support the ContainsValue function, I decided to create my own.

So I created a class named ContainsValueGenerator which derived from BaseHqlGeneratorForMethod as follows:

public class ContainsValueGenerator : BaseHqlGeneratorForMethod
{
    public ContainsValueGenerator()
    {
        SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition(() => new Dictionary<object, object>().ContainsValue(null)) };
    }

    public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
    {
        //Session.CreateQuery("from Message m where 'aDictionaryValue' in elements(m.Dictionary)"); 

        HqlTreeNode hqlTreeNode = treeBuilder.Elements(); // TODO include dictionary here? 
        return treeBuilder.In(visitor.Visit(arguments[0]).AsExpression(), hqlTreeNode);
        //return treeBuilder.In(visitor.Visit(arguments[0]).AsExpression(), treeBuilder.Indices(visitor.Visit(targetObject).AsExpression())); ContainsKey Method implementation 
    }
}

I looked at the source code from Nhibernate and took the ContainsKey method implementation. The Hqlsyntax is very similar to the ContainsKey syntax. The only thing that needs to change (I guess) is, instead of the treebuilder.Indices(), I have to use the treebuilder.Elements(shown in comments).

That’s where I’m stuck; I can’t provide the ElementsExpression with my own Dictionary.
How can I create a HqlTreeNode that takes advantage of a Elements expression?

Thanks in advance,
Rob Van Pamel

  • 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-03T12:56:30+00:00Added an answer on June 3, 2026 at 12:56 pm

    The solution is to create your own HqlElements class that inherits from HqlExpression. In fact it is just a copy of the HqlIndices class.

    public class HqlElements : HqlExpression
    {
        public HqlElements(IASTFactory factory, HqlExpression dictionary)
            : base(HqlSqlWalker.ELEMENTS, "elements", factory, dictionary)
        {
        }
    }
    

    The tricky part is to add this as property to the HqlTreeBuilder class. Therefore you can use an extension method. This contains one ugly piece of code the get the factory of the HqlTreeBuilder with reflection.

    public static class HqlTreeBuilderExtensions
    {
        public static HqlElements Elements(this HqlTreeBuilder treeBuilder, HqlExpression dictionary)
        {
            var factory = (IASTFactory) treeBuilder.GetType().GetField("_factory", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(treeBuilder);
    
            return new HqlElements(factory, dictionary);
        }
    }
    

    Your generator can now be changed to:

    public class ContainsValueGenerator : BaseHqlGeneratorForMethod
    {
        public ContainsValueGenerator()
        {
            SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition(() => new Dictionary<object, object>().ContainsValue(null)) };
        }
    
        public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
        {
            return treeBuilder.In(visitor.Visit(arguments[0]).AsExpression(), treeBuilder.Elements(visitor.Visit(targetObject).AsExpression()));
        }
    }
    

    The best solution would be that this piece of code would make it into NHibernate itself. Maybe I’ll add a patch to GitHub for that.

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

Sidebar

Related Questions

I am developing an iphone project in which I have to use Walrus S3
I have a WPF Linq-to-SQL project which I have been developing using MS Visual
I have a Sitecore/ASP.NET projects that I'm developing. Today at some point I inadvertently
I have a project that uses two third party libraries, both of which make
I have a python app, that I'm developing. There is a need to use
I have been developing a WCF project that will expose web services (HTTP-based) that
I have a project that I am developing built off CodeIgniter. The main part
I have a working Android tuner project app that I want to use as
i am developing a project in which i have used many classes. for creating
i am developing a project that its domain is meaningful in my native language.

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.