I have a class
public class TestClass
{
public int? a { get; set; }
public int? b { get; set; }
public TestClass(int _a, int _b)
{
a = _a;
b = _b;
}
}
And for this class, I generate a list with elements of it.
List<TestClass> lista = new List<TestClass>
{
new TestClass(1,2),
new TestClass(2,3),
new TestClass(1,3)
};
Now, I would like to generate a method that according to the options that the user selects, allows me to return or all the elements of the list (if I do not select anything), or those that comply with what the user selected.
If I have that class with only those two properties I could do something like this:
List<TestClass> resul;
if (seleccionA == null && seleccionB == null)
{
resul = lista;
}
if (seleccionA == null && seleccionB != null)
{
resul = lista.Where(v => seleccionB == v.b).ToList();
}
if (seleccionA != null && seleccionB == null)
{
resul = lista.Where(v => seleccionA == v.a).ToList();
}
if (seleccionA != null && seleccionB != null)
{
resul = lista.Where(v => seleccionA == v.a && seleccionB == v.b).ToList();
}
But if they were more than two properties, the if is getting worse. How can I solve this in a more efficient way to write in the code?