I have a list of objects. Each object has n properties. I have a another list with m [1..n] property names.
Two questions:
- How do I filter the list of objects by checking if one of the properties in the other list contains a string?
- How do I filter the list of objects by checking if ANY property contains a string?
Here is the object class:
public class MyModel
{
public string POne { get; set; }
public string PTwo { get; set; }
public string PThree { get; set; }
}
In pseudocode in would be something like:
New List filtered
Foreach object o in myObjectList
If o.POne, o.PTwo or o.PThree contains string "test"
filtered.Add(o)
Else
Next
I tried to adapt the code from this post, but could not get a working. Another thought would be to create a nested list with all property values. In that scenario I get the filtering working with the following line:
List<List<string>> filtered = testList.Where(q => q.Any(a => a.Contains(testString))).ToList();
But isn't that creating a lot of overhead by creating all those extra lists? Performance is a concern for me.