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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:00:11+00:00 2026-05-19T03:00:11+00:00

I’m trying to create a method which create the where predicate to use later

  • 0

I’m trying to create a method which create the where predicate to use later on a linq where method (Linq2NHibernate).
what I have here is a method that perform the creation of an expression that will be used in a query Linq.
Let’s see some code

private Expression<Func<Model.FattureEmesse, bool>> getUserFilterExpression(FattureFilter filterStructure)
{
  Expression<Func<Model.FattureEmesse, bool>> dataDocumentoFilter = f =>
     true;

  Expression<Func<Model.FattureEmesse, bool>> dataFineValiditaFilter = f =>
     true;

  var userFilterExpression = dataDocumentoFilter
     .And(dataFineValiditaFilter)
     .And(dataImportazioneFilter);

  return userFilterExpression;            
}

The expression will be more complex than a simple “always true”, but for the problem I have I got the same behaviour.
The .And you see is an extension method (I found here on SO for real)

 internal static class PredicateExtensions
{
    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2) where T : Model.DomainModelObject
    {
        InvocationExpression invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());

        return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body, invokedExpression), expression1.Parameters);
    }
}

Now I have a Business Logic class with a method that collect the Expression created and pass it to the DAL manager.
That method simply calls

var userFilterExpression = getUserFilterExpression(filterStructure);
var securityFilterExpression = new BL.SecurityManager().getFilterExpression<Model.FattureEmesse>(user);
var totalFilterExpression = userFilterExpression.And(securityFilterExpression);

As you can see I retrieve another expression from the Securitymanager (in case you doubt, the problem occour even if I avoid to chain toghether the 2 expression)
and then call

var filteredFatture = new GenericDalManager().getFilteredList<Model.FattureEmesse>(maximumRows, startRowIndex, totalFilterExpression);

This is where everything is put toghether to make the query

public class GenericDalManager
{
   internal IList<T> getFilteredList<T>(int maximumRows, int startRowIndex, Expression<Func<T, bool>> expressionResult) where T : Model.DomainModelObject
   {   
      using (var session = PersistenceManager.Istance.GetSession())
      {
         var items = (from o in session.Linq<T>()
                      orderby o.Id
                      select o);

         var filteredItems = items.Where(expressionResult)
                                  .Skip(startRowIndex)
                                  .Take(maximumRows);                    

         return filteredItems.ToList();
      }  
   }
}

So, when I call the method that perform the query, it gaves e back an error in the .ToList() call you see in the last line.
The error is
Object reference not set to an instance of an object.
And it occurs in NHibernate.Linq.
The problem seems to be related to the And extension method, since it works if I make a simple expression, without any chaining.
What’s wrong?
If I simply compile the expression befor passing it to the genericDalManager.getFilteredList method, everything works, but it will not evaluate the expression as a query on the database (obviously the “always true” expression won’t generate any interesting where clause, but I need this to work in order to do some more complex filter).
The strange thing is that if I won’t chain the two “always true” expression in the method getUserFiltreExpression, but I return only the first one, everything works fine. Even if after the same expression will be chained with the result of BL.SecurityManager().getFilterExpression(user) calling.
And by the way, for testing this issue, this last call return another “always true” expression.

Anyone help please… I’m getting mad!

  • 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-05-19T03:00:11+00:00Added an answer on May 19, 2026 at 3:00 am

    I guess the problem is that you And with the result of the expression (after invoke) and not with the expression itself. That expression can probably not be parsed. You might also have to you AndAlso (logical and) insead of And (bitwise and) and maybe rebind the parameters

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.