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

  • Home
  • SEARCH
  • 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 4014498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:30:16+00:00 2026-05-20T09:30:16+00:00

public static bool Compare<T>(this T source, T other, params Expression<Func<T>>[] propertiesToSkip) { PropertyInfo[] sourceProperties

  • 0
public static bool Compare<T>(this T source, T other, params Expression<Func<T>>[] propertiesToSkip)
{
    PropertyInfo[] sourceProperties = source.GetType().GetProperties();
    List<string> lstPropertiesToSkip = (from x in propertiesToSkip select ((MemberExpression)((UnaryExpression)x.Body).Operand).Member.Name).ToList();
    return !(sourceProperties.Any(sourcePropertyInfo => !lstPropertiesToSkip.Contains(sourcePropertyInfo.Name) && (!sourcePropertyInfo.GetValue(source, null).Equals(sourcePropertyInfo.GetValue(other, null)))));
}

I want to be able to call this function by.

var source = new MyClass();
var other = new MyClass();
source.Compare<MyClass>(other, ()=>SkipProperty1, ()=>SkipProperty2);

The problem is ()=>SkipProperty1 and ()=>SkipProperty2 are not of type MyClass.
I need:

Compare <A,B,C,D...>  
params Expression<Func<T>>[] where T is A,B,C,D,E...

I have looked at AutoMapper, with fluent syntax.

public IMappingExpression<TSource, TDestination> ForMember(string name, Action<IMemberConfigurationExpression<TSource>> memberOptions);

Mapper.CreateMap<MyClass, MyClass>()
            .ForMember(dest => dest.Property1, opt => opt.Ignore())
            .ForMember(dest => dest.Property2, opt => opt.Ignore()));

– Can I change the signature of Compare to make the function work?

– Is there other way to solving this? For example using fluent syntax

Note I don’t want to send property names as strings.

  • 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-20T09:30:16+00:00Added an answer on May 20, 2026 at 9:30 am

    One option would be to change the signature of your param arguments to be lambda expressions that return object. Something like this:

    public static bool Compare<T>
      (this T source, T other, params Expression<Func<object>>[] propertiesToSkip)
    

    This should work fine, because any .NET type can be cast or boxed to object. You’ll need to slightly adjust your code to extract PropertyInfo from the lambda expressions (because there will be additional boxing or cast).

    Using fluent syntax should work as well. You’ll need some type to pass around:

    class SkipComparison<T> {
      public T Source { get; set; }
      public T Other { get; set; }
      public List<PropertyInfo> PropertiesToSkip { get; set; }
    
      public SkipComparison<T> Skip<R>(Expression<Func<R>> f)
        // TODO: Extract property info, add it to 
        // 'PropertiesToSkip' and 'return this;'
      public bool Run()
        // TODO: Actually perform the comparison here
    }
    

    Your Compare method will return a new instance of this class with just Source and Other set and you’ll use the Skip method to add additional properties to be skipped.

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

Sidebar

Related Questions

Is this thread safe? private static bool close_thread_running = false; public static void StartBrowserCleaning()
I have a function defined like this: public static void ShowAbout(Point location, bool stripSystemAssemblies
I have an extension method as follows: public static bool SatisfiesSomeCondition(this Post post, SomeObj
I like c#, but why can I do : public static bool Initialized {
[DllImport(user32.dll)] public static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw); [DllImport(user32.dll,
public static IQueryable<TResult> ApplySortFilter<T, TResult>(this IQueryable<T> query, string columnName) where T : EntityObject {
I want to build an expression for a Where clause Expression<Func<T, bool>> on a
public static bool IsDivisible(double p, double n, double r, double k) { double x
I have a method called isStringOnlyWhitespace(): public static bool isStringOnlyWhitespace(string toEval) { string stringNoWhitespace
public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);

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.