In my Person class, I have fields like firstname, lastname, nickname, etc.
I want to write the search code dynamically, sometimes by the name "firstname", and sometimes by the field "nickname".
In the usual manner, the code will be:
If(SearchBy == "firstname")
{
Person result = ListOfPerson.Where(p => p.firstname== "exp").FirstOrDefault();
}
else If(SearchBy == "nickname")
{
Person result = ListOfPerson.Where(p => p.nickname== "exp").FirstOrDefault();
}
But the code I want to write should be like this: (to save, if every time)
Object someVariable = "firstname";
Person result = ListOfPerson.Where(p => p.someVariable == "exp").FirstOrDefault();
Can anyone know if this is possible?
source
share