Assuming we have the following table:
Person:
PersonID,
Name,
Age,
Gender
And we provide a search function that allows users to search a table according to name and / or age.
The hard part of writing an SQL query (or LINQ) is that users can choose to search for either a field or any field or field. If he wants to find everything, then he just needs to leave the text field empty.
The logic for this can be written as follows:
var p;
if(Name_TextBox=='')
{
p=from row in person
select row ;
}
else
{
p= from row in person
where row.Name=Name_TextBox
select row ;
}
Now after a while the code becomes very long and messy ... How to compress the above into a single request using no if-else ?
source
share