How do I search on two combined fields. The search should happen on the SQL end if possible.
So say I have a customer table with first name and last name. I would like users to be able to search on both of those columns using a single search box.
My query currently looks like this:
var query = DbContext.Customers
.Where(c => c.FirstName.Contains(search) || c.LastName.Contains(search));
but it should be something like
var query = DbContext.Customers
.Where(c => c.FullName.Contains(search));
It is not possible unless you have FullName
column also mapped. The way around this problem can be String.Concat which is allowed in Linq-to-entities:
var query = DbContext.Customers
.Where(p => String.Concat(p.FirstName, " ", p.LastName)
.Contains(search));
You could use a computed column in the database and map that e.g.
alter table Customer add FullName AS FirstName + ' ' + LastName
(Not pretty I know)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With