0

I have a list of objects. Each object has n properties. I have a another list with m [1..n] property names.

Two questions:

  1. How do I filter the list of objects by checking if one of the properties in the other list contains a string?
  2. 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.

4
  • You have different classes? Can you modify them? Is the different classes the only obstacle to implement the pseudo code? How much of concern is performance (in particular given that you seem willing to use reflection)? Commented Apr 7, 2021 at 9:31
  • @Theraot The filtering must be generic. It may be used to filter different classes. It is basically a table component I'm showing in a user interface. The filtering must be performed "live" when the user enters a letter. Thus, performance is critical. I'm fairly new to C# and thought reflection was the only way to achieve that. I would also be able to edit the class of the model if that helps I do not know which properties will be shown until the component is used in a context. Commented Apr 7, 2021 at 9:41
  • I asked about modifying the class, because it could be addressed as in the current answer. If you have multiple classes, you can handle that with an interface. If there are multiple classes and you can't modify them, we could be making helper classes with a factory. If you don't know the classes at all, then yes, reflection. We would then look into code emitting code and caching it to optimize it (there is no point in reflecting the same class multiple times). Commented Apr 7, 2021 at 9:50
  • List are probably using links to the source strings and not make a copy of the strings. I like using dictionaries and nested dictionaries list Dictionary<string,Dictionary<string,List<string>>> Commented Apr 7, 2021 at 10:22

1 Answer 1

2

Make your Class responsible for determing if it matches or not via a method

public class MyModel
{
    public string POne { get; set; }
    public string PTwo { get; set; }
    public string PThree { get; set; }

    public bool DoesMatch(string tomatch)
    {
        return POne.Equals(tomatch) || 
           PTwo.Equals(tomatch) ||
           PThree.Equals(tomatch);
    }
}

Then you can do this

var filtered = testList.Where(q => q.DoesMatch("string to check"));
Sign up to request clarification or add additional context in comments.

3 Comments

Additionaly, if the code does not concern the model itself but some other layer, you could make use of extension functions in C#.
How do I handle the case when only certain propertíes of the model are to be checked? E.g., POne in one case and PTwo + PThree in the other case
@UnusualWays You can have different to check the different properties, and then call the appropriate method. Or have one method and the class needs to determine what needs to be checked first, and then check them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.