Is there any way to refactor this code so that it can omit unnecessary WHEREs and JOINs if the values passed into the function are null (this code works just fine right now if all parameters are passed in)? The "SearchItems" and "ItemDistance" functions are table-level functions for performing fulltext search and distance calculations, respectively.
public IQueryable<ItemSearchResult> Search(string keywords, int? category, float? latitude, float? longitude)
{
return from item in _db.Items
join searchItems in _db.SearchItems(keywords)
on item.ItemId equals searchItems.ItemId
join itemDistance in _db.ItemDistance(latitude.Value, longitude.Value)
on item.ItemId equals itemDistance.ItemId
where item.Category == category.Value
select new ItemSearchResult()
{
Item = item,
Distance = itemDistance.Distance
};
}