Hello. There was a very difficult question that I have never encountered. I need to create a method that will understand by what property the filter needs to be made. Ie, let's say I have a class
public class User { public string Name {get; set;} public string Nick {get; set;} } And I need to pull some users out of the database, but the criterion is not known in advance, in the query Name or Nick can be null.
At the moment it looks like this:
//это часть когда находится в классе user IQarable<User> query ... тут создается query и передается в метод ниже ... if (!string.IsNullOrEmpty(Name)) { query = query.Where(x => x.VenueName.Contains(Venue)); } if (!string.IsNullOrEmpty(Nick)) { query = query.Where(x => x.City.Contains(City)); } //и так далее Inside the If blocks, there is some other check, which is why I try to bring this into method 1, but that's not the point.
I'm trying to make a method that takes a query and a property in the form of a string, which needs to be executed Where (...), so that it would look like this
if (!string.IsNullOrEmpty(Name)) { query = SearchMethod(query, "Name", "Jhon"); } I can’t imagine how I can replace the Where(x => x./*тут свойство, которое каким-то образом определено*/.Contais("SearchingValue")) is something else that can be calculated by the property I search and substitute it in this expression. By reflection I was able to get only the property itself.
Type t = this.GetType(); PropertyInfo prop = t.GetProperty("EventName"); I ask your help in solving this problem.
prop.GetValue(this). - Grundy