I would make a filter to filter my data using next parameters:
- First name
- Last name
- Language
- Profile
- A minimum date of registration
- A maximum date of registration
- Success
All filter options are not required for the user. Here could you find my C# code.
private IEnumerable<Registration> Filter(string voornaam, // first name
string achternaam, // last name
int? language,
int? profiel, // profile
DateTime? vanaf, // min date
DateTime? tot, // max date
bool? geslaagd) // success
{
IEnumerable<Registration> all = All(); // <-- this gets all registrations from
// the database.
if (!string.IsNullOrEmpty(voornaam))
{
all = (from r in all
where r.FirstName.ToLower().StartsWith(voornaam.ToLower().Trim())
select r).ToList();
}
if (!string.IsNullOrEmpty(achternaam))
{
all = (from r in all
where r.LastName.ToLower().StartsWith(achternaam.ToLower().Trim())
select r).ToList();
}
if (language != null)
{
all = (from r in all
where r.LanguageId == language.Value
select r).ToList();
}
if (profiel != null)
{
all = (from r in all
where r.ProfileId == profiel.Value
select r).ToList();
}
if (vanaf != null)
{
all = (from r in all
where r.RegistrationDate >= vanaf.Value
select r).ToList();
}
if (tot != null)
{
all = (from r in all
where r.RegistrationDate <= tot.Value
select r).ToList();
}
if (geslaagd != null)
{
all = (from r in all
where r.Success == geslaagd.Value
select r).ToList();
}
return all;
}
The function All()
gets all the registrations from the database. Next I check if the attributes are not null (or empty for the strings) and if true I set the variable all
equal to the return values of the linq query's. After all I return all
and load my view.
This code works for all possibilities and got a low fail percentage. Of course my code is too long and will make this code shorter. How could I do this?