lundi 9 mars 2015

Compare property called by name with type T in where clause lambda expression

I will start by showing my code :



public IEnumerable<MessageSummary> GetMessagesBy<T>(string propertyName, T parameter)
{
tempMessages = mylist.Where(x => EqualityComparer<T>.Default.Equals((T) (x.GetType().GetProperty(propertyName).GetValue(x, null)), parameter)).ToList();
...
}


I would like to get a list of object in mylist, depending the property given as parameter, and compare it to an object of variable type. It breaks on this line, saying that there were a problem in the where clause.


But, if I do this :



if (EqualityComparer<T>.Default.Equals((T)mylist.First().GetType().GetProperty(propertyName).GetValue(mylist.First(), null), parameter))
{ ... }


It works well...


What's wrong in my where clause ?


Thanks a lot for your answer






Aucun commentaire:

Enregistrer un commentaire