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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:14:55+00:00 2026-05-25T14:14:55+00:00

I have a feeling the answer is no after reading through documentation. However, I

  • 0

I have a feeling the answer is no after reading through documentation. However, I am trying to find out whether it’s possible to know if a type supports relational operators, such as: <, >, >= etc…

I am looking to do something like:

Func<MyObject, object> greaterThan = obj => obj > 10;
var results = myList.Where(greaterThan).ToList();

As I may be searching on different properties, it might be decimal, int or float. I am doing this in an MVC action so, as far as I know, it can’t support generics. I can obviously move it out and have a generic implementation, but I’d like to know this answer first.

To elaborate, implicit operator overloading isn’t feasible. The reason being is that property is not known until runtime (sorry should of mentioned that). Consequently nor can generics be used. Given any property, I need to check that it’s not 0. This is a somewhat contrived example of my current implementation that seems to be working (not fully tested):

Func<MyObject, IComparable> prop = obj =>  isOn ? obj.DecimalVal : obj.IntValue;
Func<MyObject, bool> clause = obj => prop(obj).CompareTo(0) != 0;
var results = myList.Where(clause);

I’m just hoping that CompareTo will return 0; even if the prop evaluates a decimal which has a value of 0, when compared to the int 0 – despite these being different types.

UPDATE

I clearly had my hopes set too high, the above approach failed. Therefore, the solution I am using for now is not desirable but it’s working. I would love to hear a more elegant approach.

Func<MyObject, dynamic> prop = obj =>  isOn ? obj.DecimalVal : obj.IntValue;
Func<MyObject, bool> clause = obj => prop(obj) != 0;
var results = myList.Where(clause);
  • 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-25T14:14:56+00:00Added an answer on May 25, 2026 at 2:14 pm

    No. In C#, operators are static members of the types, so they can’t be part of an interface.

    There is one way for your code to work in C# 4: dynamic:

    Func<dynamic, bool> greaterThan = obj => obj > 10;
    var results = myList.Cast<dynamic>().Where(greaterThan).ToList();
    

    This will work whether myList is a collection of ints, doubles or decimals. (But beware, results is of type List<dynamic>.)

    But .Net actally provides some interfaces that can be used for the same purposes operators are commonly used for: comparing for equality (IEquatable<T>) and comparing for which element is greater and which is smaller (IComparable<T>). With those, you can write code like this:

    static IEnumerable<T> GreaterThan<T>(this IEnumerable<T> collection, T item)
        where T : IComparable<T>
    {
        return collection.Where(x => x.CompareTo(item) > 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feeling that I already know the answer to this one, but
I have a feeling I already know the answer people are going to give,
ANSVER: After reading and reading and reading a little more documentation. I have decided
I have a feeling the answer to this is going to be not possible,
I have a feeling I know the answer to this question, but I figure
I have the feeling the answer to this question is no but I figured
I have the feeling that is easy to find samples, tutorials and simple examples
I have a feeling that there must be client-server synchronization patterns out there. But
I have a feeling this is a repost but I can't seem to find
I have a feeling the answer to this is no, but using .Net 4.0's

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.