How to include an AND () expression that validates a property and its value

I would like to add a check to our repository, which filters all objects on companyId, if there is one, and if it matches the given value.

So where do we have:

public T First<T>(Expression<Func<T, bool>> expression) where T : EntityObject  
{  
   var set = GetObjectSet<T>();  
   return set.FirstOrDefault<T>();  
}  

I would like to add a line that looks somewhere there ...

express.And("Check for CompanyId property if it exists then make sure it = 3");  

Any ideas on how to do this?
Thank:)

+3
source share
3 answers

If you are looking for a function that you can use to bind to a company identifier, check your expression if there is a company entity on the entity, this should do the trick:

public static Expression<Func<T, bool>> CheckPropertyIfExists<T, TProperty>(Expression<Func<T, bool>> expression, string propertyName, TProperty propertyValue)
{
    Type type = typeof(T);
    var property = type.GetProperty(propertyName, typeof(TProperty));
    if(property == null || !property.CanRead)
        return expression;

    return expression.Update(
        Expression.And( // && 
            Expression.Equal( // ==
                Expression.MakeMemberAccess(expression.Parameters[0], property), // T parameter.{propertyName}
                Expression.Constant(propertyValue) // specified propertyValue constant
            ),
            expression.Body // Original expression
        ),
        expression.Parameters
    );
}

You can use it as follows:

public T First<T>(Expression<Func<T, bool>> expression, int companyId)
{
    var set = GetObjectSet<T>();  
    return set.FirstOrDefault<T>(CheckPropertyIfExists(expression, "CompanyId", companyId));  
}

First , .

, , ( IQueryable):

public static ObjectQuery<T> FilterByPropertyIfExists<T, TProperty>(this ObjectQuery<T> query, string propertyName, TProperty propertyValue)
{
    Type type = typeof(T);
    var property = type.GetProperty(propertyName, typeof(TProperty));
    if(property == null || !property.CanRead)
        return query;

    var parameter = Expression.Parameter(typeof(T), "x");
    Expression<Func<T, bool>> predicate = (Expression<Func<T, bool>>)Expression.Lambda(
        Expression.Equal( // ==
            Expression.MakeMemberAccess(parameter, property), // T parameter.{propertyName}
            Expression.Constant(propertyValue) // specified propertyValue constant
        ),
        parameter
    );
    return query.Where(predicate);
}

, LINK stanard ( , ).

. :

from x in repository.Clients.FilterByPropertyIfExists("Company", 5)
where x == ???
select x.Name;

[EDIT]

( , ), ObjectQuery ( ObjectQuery ObjectSet):

public static class QueryExtensions
{
    public static IQueryable<T> FilterByPropertyIfExists<T, TProperty>(this IQueryable<T> query, string propertyName, TProperty propertyValue)
    {
        Type type = typeof(T);
        var property = type.GetProperty(
            propertyName, 
            BindingFlags.Instance | BindingFlags.Public, // Must be a public instance property
            null, 
            typeof(TProperty), // Must be of the correct return type
            Type.EmptyTypes, // Can't have parameters
            null
        );
        if (property == null || !property.CanRead) // Must exist and be readable
            return query; // Return query unchanged

        // Create a predicate to pass to the Where method
        var parameter = Expression.Parameter(typeof(T), "it");
        Expression<Func<T, bool>> predicate = (Expression<Func<T, bool>>)Expression.Lambda(
            Expression.Equal( // ==
                Expression.MakeMemberAccess(parameter, property), // T parameter.{propertyName}
                Expression.Constant(propertyValue) // specified propertyValue constant
            ),
            parameter
        );
        return query.Where(predicate); // Filter the query
    }

    public static ObjectQuery<T> FilterByPropertyIfExists<T, TProperty>(this ObjectQuery<T> query, string propertyName, TProperty propertyValue)
    {
        var filteredQuery = FilterByPropertyIfExists((IQueryable<T>)query, propertyName, propertyValue);
        return (ObjectQuery<T>)filteredQuery; // Cast filtered query back to an ObjectQuery
    }
}
+4

, , :

public interface ICompanyFilterable
{
  int CompanyId { get; set; }
}

public partial class YourEntity : ICompanyFilterable
{
    ....
}

public static IQueryable<T> FilterByCompanyId<T>(this IQueryable<T> query, int companyId)
    where T : ICompanyFilterable
{
    return query.Where(e => e.CompanyId == companyId);
}
+1

Bennor, Thank you very much for your message VERY helpful.

I took what you placed there and created an extension method that adds an “And” filter to an existing expression.

    public static Expression<Func<T, bool>> AddEqualityCheck<T, TProperty>(this Expression<Func<T, bool>> expression, string propertyName, TProperty propertyValue)
    {
        Type type = typeof(T);
        var property = type.GetProperty(
            propertyName,
            BindingFlags.Instance | BindingFlags.Public,
            null,
            typeof(TProperty),
            Type.EmptyTypes,
            null
        );

        if (property == null || !property.CanRead)
        {
            return expression;
        }
        else
        {
            var equalityExpression = Expression.Equal(
                Expression.MakeMemberAccess(expression.Parameters[0], property),
                Expression.Constant(propertyValue)
                );
            var andEqualityExpression = Expression.And(equalityExpression, expression.Body);
            return expression.Update(andEqualityExpression, expression.Parameters);
        }
    }
0
source

Source: https://habr.com/ru/post/1787443/


All Articles