I have a table called Items(Name,Description)
The user can search for items based on the name, the description or both. (The actual table has about 15 fields which can be searched on, but in this example, just keeping it to two).
This searches for both the name and description. But I need it to be one, the other or both.
var source = this.DbContext.Items; IQueryable<Item> items = source.Where(a => a.Name.Contains(item.Name)); items = items.Where(a => a.Description.Contains(item.Description)); return items.ToList(); This is the best I have come up with, but that second line seems kind of funky.
internal List<Item> Search(Item item) { var source = this.DbContext.Items; IQueryable<Item> items = source.Where(a=> a == a); if(!string.IsNullOrWhiteSpace(item.Name)) items = items.Where(a => a.Name.Contains(item.Name)); if(!string.IsNullOrWhiteSpace(item.Description)) items = items.Where(a => a.Description.Contains(item.Description)); return items.ToList(); }
IQueryable<Item> items = source.Where(a=> a == a);?