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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:24:00+00:00 2026-05-31T03:24:00+00:00

I have class X that implements an interface IX . I also have a

  • 0

I have class X that implements an interface IX. I also have a repository class dedicated for X, which uses lambda expresions as parameters:

public interface IX
{
}

public class X : IX
{
    ....
}

public class XRepository : IRepository<X>
{
    public IEnumerable<X> Filter(Func<X, bool> filterFunc)
    {
        ...
    }
}

I need to make the repository class work with the interface IX, therefore I add IRepository<IX> to the interfaces being implemented:

public class XRepository : IRepository<X>, IRepository<IX>
{
    public IEnumerable<X> Filter(Func<X, bool> filterFunc)
    {
        ...
    }
    public IEnumerable<IX> Filter(Func<IX, bool> filterFunc)
    {
        // I need to call the same filter method as above, but 
        // in order to do so I must convert the Func<IX, bool> to Func<X, bool>.
    }
}

I must convert the Func<IX, bool> to Func<X, bool>, but since the code is written in C# 3.0 using .NET 3.5, I cannot benefit from Type covariance, which was introduced in 4.0.

A simple solution could be to use Func<X, bool> newFunc = x => filterFunc(x);, where filterFunc is of type Func<IX, bool>. This would compile and one might expect it to run fine, but I assume it will not. The problem is that I am using 3rd party framework for the filter implementation, namely FluentNhibernate. I know it uses expression trees to strip the passed into the lambda expression member access condition (like x => x.Name == "John") in order to build native SQL query (like WHERE Name = 'John'). The above solution would produce a Func<X, bool> that is not such expression and I fear it will fail to translate. So I need to create the same lambda expression but with the compatible type. Knowing that X implements IX, it is obvious that any code inside a Func<IX, bool> will work for objects of type X. It is not obvious for me, however, how can I perform this conversion.

I assume this can be done using expression trees. I also fear my performance will suffer greatly. Even if I decide to have another solution to my scenario, I will still appreciate the suggested way to translate one lambda into a similar another.


Edit:

To clarify more about the issue I am experiencing, I wrote the following test, simulating the real-life scenarion I am facing:

    Func<IX, bool> filter = y => y.Name == "John";
    Func<X, bool> compatibleFilter = y => filter(y);


    ...
    // Inside the Filter(Func<X, bool> filter method)
    using(var session = nhibernateSessionFactory.OpenSession())
    {

        IEnumerable<X> xx = session.Query<X>().Where(z => compatibleFilter(z)).ToList();
    }

so, at the ToList() method I receive the following exception

Unable to cast object of type 'NHibernate.Hql.Ast.HqlParameter' to type 'NHibernate.Hql.Ast.HqlBooleanExpression'.

This confirms my assumption that Flunet NHiberante cannot correctly handle the compatibleFilter argument.

So what I want is a way to convert the Func to Func or as suggested by John Skeet, an Expression<Func<IX, bool>> to an Expression<Func<X, bool>> which have the same body (y => y.Name = "John").


Edit 2:

Finally I made it happen! The correct way is not to use Func<X, bool>, but Expression<Func<X, bool>>.

 Expression<Func<IX, bool>> filter = y => y.Name == "John Skeet";
 Expression<Func<X, bool>> compatibleFilter = Expression.Lambda<Func<X, bool>>(
    filter.Body, 
    filter.Parameters);

This produces the correct SQL query.IX, bool

  • 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-31T03:24:02+00:00Added an answer on May 31, 2026 at 3:24 am

    A simple solution could be to use Func<X, bool> newFunc = x => filterFunc(x); where filterFunc is of type Func<IX, bool>. This would compile and one might expect it to run fine, but I assume it will not.

    Why assume, when you can test? It should work absolutely fine. After all, you’re passing an argument of type X for a parameter of type IX, which causes no type safety concerns.

    You’ll then need to convert from IEnumerable<X> to IEnumerable<IX>, which can be done with Cast for instance:

    public IEnumerable<IX> Filter(Func<IX, bool> filterFunc)
    {
        Func<X, bool> newFilter = x => filterFunc(x);
        return Filter(newFilter).Cast<IX>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that implements an Interface with some methods. I also have
I have a class MyDoublyIterator<E> that implements the Iterator<E> interface. Also, it has a
I have a class that implements specific interface ( IOrganicEnvironment<T, K> ) public class
Given: I have an interface. I have only class that implements that interface. Question:
I have a class that implements the IDisposable interface. I am using a webclient
Let's say I have a class that implements the IDisposable interface. Something like this:
I have 2 instances of a class that implements the IEnumerable interface. I would
I'm currently writing a class that implements the SeekableIterator interface and have run into
I am trying to create a class that implements the IUnknown interface. I have
I have a class that implements IComparable. public class MyClass : IComparable<MyClass> { public

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.