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