Using EF6 + SQL Server 2008 R2 + .NET Fx 4.5.
I need to search a particular string in multiple fields of my entity and its related entities and return the records where the search string is found in ANY of the fields. Here's the relevant code:
Dim Query = MyDB.items.AsQueryable()
Query = Query.Where(Function(r) _
r.stock_no.Contains(searchString) OrElse _
r.serial_number.Contains(searchString) OrElse _
r.catalog_item.description.Contains(searchString) OrElse _ r.catalog_item.category.Contains(searchString) OrElse _
r.item_status.name.Contains(searchString) OrElse _
r.notes.Contains(searchString))
If r.issued_to_company_id.HasValue Then 'This is a nullable field (0-1 relation)
Query = Query.OR_FUNCTION(Function(r) r.issued_to_company.name.Contains(searchString))
End If
The problem is that there is no OR_FUNCTION in LINQ. You can simulate an optional AND_FUNCTION by calling another Where
on the Query
object, but I need to do OR here.
I have tried to use If
and IIf
inside the predicate too, but seemingly can't do that without introducing a compile- or run-time error. What's the correct way of doing this?