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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:46:28+00:00 2026-06-07T13:46:28+00:00

Just a quick and short one, this time. Func<T,TResult> is contravariant ( EDIT :

  • 0

Just a quick and short one, this time. Func<T,TResult> is contravariant (EDIT : The Type Parameter T is). Now, I don’t work with Func<T,TResult>, but rather with Expression<Func<T,TResult>>, and seem to have reached a dead end. UPDATE – FULL CODE SAMPLE :

public interface IColoredObject
{
    string Color { get; }
}

public class Item : IColoredObject
{
    public string Color { get; set; }

    public double Price { get; set; }
}

public partial class MainWindow : Window
{
    private IList<Item> _items;

    public IList<Item> Items
    {
        get
        {
            if (_items == null)
            {
                _items = new List<Item>();
                _items.Add(new Item() { Color = "black" });
                _items.Add(new Item() { Color = "blue" });
                _items.Add(new Item() { Color = "red" });
            }
            return _items;
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        Expression<Func<IColoredObject, bool>> filter = x => x.Color == "black";
        Item i = Get(filter);
    }

    public Item Get(Expression<Func<Item, bool>> filter)
    {
        return Items.AsQueryable().Where(filter).FirstOrDefault();
    }
}

The call is made using an Expression<Func<IColoredObject, bool>> as argument and should, if I haven’t misunderstood contravariance, work, because IColoredObject is less derived that Item.

What I get is a conversion Exception saying something like

cannot convert

System.Linq.Expressions.Expression`1[System.Func`2[MyNs.IColoredObject,System.Boolean]]

To

System.Linq.Expressions.Expression`1[System.Func`2[MyNs.Item,System.Boolean]]

Is there any way of fixing this and getting it to work?

EDIT:

Since there’s some inaccuracy in what I’ve said, here’s more background. Code sample updated. Furthermore, I checked what MSDN said about Func<T, TRes>:

public Item GetFunc(Func<Item, bool> filter)
{
    return Items.AsQueryable().Where(filter).FirstOrDefault();
}

As indicated by MS, this can be used with a contravariant Type param, as listed below:

 Func<IColoredObject, bool> filterFunc = x => x.Color == "black";
 GetFunc(filterFunc);

Which again makes me wonder why this works for Func<T, TRes> but not for Expression<Func<T, TRes>>…

FINALLY…

The checked answer was selected because it is what I eventually did. As I said somewhere in the comments below, the Get-Method utilizes NHibernate to fetch data. But obviously NHibernate has a feature of accepting queries over an interface and auto-selecting the types that implement the interface. This does not solve the issue itself, but as you can read below, there is not real solution, since what encountered here was expected behaviour.

  • 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-07T13:46:31+00:00Added an answer on June 7, 2026 at 1:46 pm

    This line:

    public Item Get(Expression<Func<Item, bool>> filter) { /* ... */  }
    

    shoud be:

    public Item Get(Expression<Func<IColoredObject, bool>> filter) { /* ... */  }
    

    You’ll have to work with the interface in this case if you want to call the Get method passing Expression<Func<IColoredObject, bool>>.

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

Sidebar

Related Questions

Just a quick one about installing a PHP website, are there any tools out
just a quick question, was trying different ways to accomplish this, but ended with
is it just me or this code in Programming Pearls is wrong (quicksort wants
Just a quick preparation for my exam, for example I have: f(x) = 5x<sup>2</sup>
Just a quick question, how do I get pypy to recognize third pary modules
Just a quick question.. Following opening a text file in a separate definition (no
Just a quick question about ReadWriteLocks in Java (specifically the ReentrantReadWriteLock implementation) as I
Just a quick question. Say a call a method like so mysql_pconnect(server,tator_w,password) or die(Unable
Just a quick question about how parameters are passed in Java... ... if ((index
Is it even possible to do something like this with just text, HTML and

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.