Hello,
In my controller, I've this action method called "SearchForContact," which takes 2 strings parameters firstName and LastName. There are 3 situations:
If both parameters are null, the view is redisplayed with an error message, prompting the user to enter at least one of the 2 parameters
if both parameters are not null, I can retrieve contacts and display the result on the view
- Unfortunately, when either of the parameters is null, I get nothing. I suspect that I did not write correctly my Linq Statements.
here's the statement that process the search:
var contacts = contactRepository.SearchForContacts(firstName, lastName).ToList();
Here are my helper methods located in my repository class (ContactRepository.cs) located in the models folder.
public IQueryable<Contact> SearchForContacts(string firstName, string lastName)
{
if (firstName == null)
return OneCriteria(lastName);
if (nom == null)
return OneCriteria(firstName);
else
return TwoCriteria(firstName, lastName);
}
private IQueryable<Contact> OneCriteria(string criteria)
{
var contacts = from contact in db.Contacts
where ((contact. firstName == criteria) ||
(contact. lastName == criteria))
orderby contact. firstName
select contact;
return contacts;
}
private IQueryable<Contact> TwoCriteria(string firstName, string lastName)
{
var contacts = from contact in db.Contacts
where ((contact. firstName == firstName) &&
(contact. lastName == lastName))
orderby contact. firstName
select contact;
return contacts;
}
Thanks for helping