You can do the same without WhereIf at all.
var result = !String.IsNullOrEmpty(searchVar) ? animals.Where(x => x.Color == "white" || x.Breed == "terrier") : new List<Animal>();
You might just want it to look up the one passed in the search query.
var result = !String.IsNullOrEmpty(searchVar) ? animals.Where(x => x.Color == searchVar || x.Breed == searchVar) : new List<Animal>(); //or return full list (animals) if search term is null
Edit: run the test in linqpad and it seems to work fine.
void Main() { SearchAnimals("white").Dump(); } public List<Animal> SearchAnimals(string searchVar) { var animals = new List<Animal>() { new Animal { Animal_ID = 1, Color = "black", Breed = "other" }, new Animal { Animal_ID = 2, Color = "white", Breed = "other" }, new Animal { Animal_ID = 3, Color = "blue", Breed = "terrier" }, new Animal { Animal_ID = 4, Color = "green", Breed = "other" } }; var result = !String.IsNullOrEmpty(searchVar) ? animals.Where(x => x.Color == "white" || x.Breed == "terrier") : new List<Animal>(); return result.ToList(); } public class Animal { public int Animal_ID { get; set; } public string Color { get; set; } public string Breed { get; set; } }
Return:
List<Animal> (2 items) Animal_ID Color Breed 2 white other 3 blue terrier
source share