I added a very simple search function on one of my sites. It contain some textboxes and a (search)button. My code behind looks like this:
private void PerformSearch()
{
DataClassesDataContext db = new DataClassesDataContext();
var result = fromquery in db.tblPerson
where
(SqlMethods.Like(query.FirstName, "%" + firstName + "%") || firstName == "")
&& (SqlMethods.Like(query.LastName,"%" + lastName + "%") || lastName == "")
orderby query.LastName, query.FirstName
select new {query.FirstName, query.LastName, query.Address, query.Phone, query.Mail };
rSearchResult.DataSource = result;
rSearchResult.DataBind();
db.Connection.Close();
}
rSearchResult is a repeater on my .aspx page to witch I bind the result.
The namespace for SqlMethods is System.Data.Linq.SqlClient.
So what is it that I do here? Well...I compare firstName and lastName (strings with search value) with data in my database. But I can also search only on first name or last name, because I also check if the string is blank. So if I leave my Firstname textbox (on my search.aspx page) blank and fill my Lastname textbox with "Andersson", this function will return everyone with Andersson as their last name since my string firstName has the value "".