I am facing trouble in fetching the filtered records in my LINQ Query to retrieve the columns dynamically based on condition and property also based on condition (ex. Contains, Equals, StartsWith, EndsWith).
I have a list of records like below -
List<Employee> employees = new List<Employee>()
{
new Employee()
{
name ="Andy", cityCriteria="Florida West", state ="NYC"
},
new Employee()
{
name = "John", cityCriteria = "West Virginia", state = "Arizona"
},
new Employee()
{
name = "Nichole", cityCriteria = "East Florida", state = "NYC"
}
};
So, this is just some sample records the data will be coming from database and it will be so many records. Now, what i want to acheive is I have to notify all the persons if any Video posted with the City matching as per the list . So, i can receive NotificationValue as City:Florida:startsWith , City:Florida: Equals , City:Florida:Contains etc and there could be State Criteria too. So, how can i filter the records dynamically in the list like if input is Starts with i should use StartsWith ex
If Input is City:Florida:startsWith -->
var result = employees.where(i=>i.CityCriteria.StartsWith("Florida").toList();
If Input is City:Florida:Contains -->
var result = employees.where(i=>i.CityCriteria.Contains("Florida").toList();
If Input is City:Florida:EndsWith -->
var result = employees.where(i=>i.CityCriteria.EndsWith("Florida").toList();
If Input is City:Florida:Equals -->
var result = employees.where(i=>i.CityCriteria.Equals("Florida").toList();
I don't want to use multiple conditions and form the Where clause . I want it to be dynamic like if i receive starts with it should replace LINQ query starts , endswith ,equals etc and also it should be flexible with dynamic column like I have to apply same logic for State,Country,Zip etc'
Please post some sample code if possible