Search code examples
c#linqexpression-trees

Create runtime predicate with information from Expression<Func<T, object>>


Assuming the following code

private readonly T entity;

public bool HasUnique<T>(Expression<Func<T, object>> property, IEnumerable<T> entities)
{
}

Where property is a property expression of T entity.

How to check if there is any element in entities with the same property value as entity has, while property is resolved from given property expression?

By now, I can get property name and its value from entity

The problem seems to be how to set up and execute at runtime a predicate? Something like

Func<T, bool> predicate = // build a predicate
var isUnique = !entities.Any(predicate);

The usage would be like this

var isUnique = HasUnique<Person>(p => p.Name, people);

Solution

  • I'm trying to figure out what the word "Unique" has to do with what you described, and your example usage doesn't involve any entity to compare the contents of people to, but I think you're going for something like this?

    public static bool HasUnique<T>(this T referenceItem, Func<T, object> property, 
                                    IEnumerable<T> entities)
    {
        var referenceProperty = property(referenceItem);
        return entities.Any(e => property(e) == referenceProperty);
    }
    

    or maybe this:

    public static bool HasUnique<T>(this T referenceItem, Func<T, object> property, 
                                    IEnumerable<T> entities)
    {
        var referenceProperty = property(referenceItem);
        return entities.Any(e => property(e).Equals(referenceProperty));
    }
    

    Or even this:

    public static bool HasUnique<T, TProp>(this T referenceItem, Func<T, TProp> property, 
                                    IEnumerable<T> entities)
    {
        var referenceProperty = property(referenceItem);
        return entities.Any(e => property(e).Equals(referenceProperty));
    }