简体   繁体   中英

Get property from object and check if it contains value

I am trying to use get property from object and check if that property contains certain value:

Method to get property looks like:

   public static object GetProp(object obj, string propName, value)
    {
        return obj.GetType().GetProperty(propName).GetValue(obj).Contains(value);
    }

This will get me object value.

And usage is (or actualy what i am trying to achive is):

     string value = this._predicateFilter.GetValueOrDefault(refName, string.Empty);
        if (!string.IsNullOrEmpty(value))
        {
            predicate.And(c => Assets.Extensions.GetProp(c, "name", "value to compare"));
        } 

I am getting an exception:

'object' does not contain a definition for 'Contains' and the best
extension method overload 'Queryable.Contains<string>
(IQueryable<string>, string)' requires a receiver of type 
'IQueryable<string>'    

And I think problem is something with linq and entity framework cant execute that method inside its body.

I could use nasty solution like:

        string firstname = this._predicateFilter.GetValueOrDefault("Firstname", string.Empty);
        if (!string.IsNullOrEmpty(firstname))
        {
            predicate.And(c => c.Firstname.Contains(firstname));
        }

        string lastname = this._predicateFilter.GetValueOrDefault("Lastname", string.Empty);
        if (!string.IsNullOrEmpty(lastname))
        {
            predicate.And(c => c.Lastname.Contains(lastname));
        }

Witch works fine but, i wold preffer less code, what am I missing?

Oh and im using

var predicate = PredicateBuilder.New<Candidate>();

from linqKit

Type.GetProperty doesn´t return the properties value , but the PropertyInfo for that property itself. What you want is to get the properties value for a given instance - in your case for obj . So use this in your GetProp -method:

return obj.GetType().GetProperty(propName).GetValue(obj);

PropertyInfo.GetValue returns an object which you are trying to use as an IQueryable<T> .

The code is telling you exactly what the error is object does not contain a (extension)method for Contains . Also in the updated code you missed the identifier off of value in the method signature and have the return type as object when you will always be returning a bool .

So fix those errors and before calling Contains cast it to an IQueryable<T> :

public static bool GetProp(object obj, string propName, string value)
{
    object o = obj.GetType().GetProperty(propName).GetValue(obj);

    IQueryable<object> queryable = o as IQueryable<object>;
    if (queryable != null)
    {
        return queryable.Contains(value);
    }

    return false; //Some other default
}

Try the following:

public static bool PropEquals<TProp>(object obj, string propName, TProp valueToTest)
{
    return EqualityComparer<TProp>.Default.
       Equals(valueToTest, obj.GetType().GetProperty(propName).GetValue(obj));
}

...

string value = this._predicateFilter.GetValueOrDefault(refName, string.Empty);
if (!string.IsNullOrEmpty(value))
{
    predicate.And(c => Assets.Extensions.PropEquals(c, "name", "value to compare"));
}

The PropEquals returns a true or false depending on whether the property whose name you supplied is equal to the value that you supplied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM