I have a large list of objects, the object contains things like string name, string address, string city.
I want to create a findEqualsMatch method like this.. where it takes in a string called varName, and then searches the variable of the object which varName is called.
that way if i do data.FindEquals("name", "tom") it searches the objects "name" property to be equal to tom, at the same time you can write "address" and it will search the address property.
public List<Datum> FindEquals(String varName, String value)
{
List<Datum> results = new List<Datum>();
foreach (Datum result in data)
{
//should search for address variable
// instead of if(result.Address == value)
if (result.varName == value)
results.Add(result);
}
return results;
}
List<Datum> newResults = data.FindEquals("address", "123 street");
the whole purpose is to query a set of factual API results and return a set that is searched on by any variable type you insert into the function.
You can use reflection. Starting from Get property value from string using reflection in C# your sample will look like:
Alternatively you can use
Func<string, Datum>
as field accessor arguments or corresponding Expression tree similar to LINQ-to-SQL (i.e. Get the property name used in a Lambda Expression in .NET 3.5)