Wednesday, 7 August 2013

Is there more elegant method for multiple conditions verification

Is there more elegant method for multiple conditions verification

Case:
ASP.NET MVC, C# and ExtJS. A user has got a filter, where he can choose
multiple values. There is 13 such filters and a user can add them or
remove from the UI.
Problem:
On the server side I've got this class for getting filters values:
public List<string> Filter1 { get; set; }
public List<string> Filter2 { get; set; }
...
public List<string> Filter13 { get; set; }
Then I select data from a database and convert it to
IEnumerable<DataClass> where DataClass is looks like below:
public string Data1 { get; set; }
public string Data2 { get; set; }
...
public string Data13 { get; set; }
Then I filter this data like this:
if (filter.Filter1 != null && filter.Filter1.Any()) {
data = data.Where(x => filter.Filter1.Contains(x.Data1));
}
...
if (filter.Filter13 != null && filter.Filter13.Any()) {
data = data.Where(x => filter.Filter13.Contains(x.Data13));
}
So there are 13 if and 13 basically same filter logic. And this code is
looks horrible. Is there any way to make more beautiful filter?

No comments:

Post a Comment