Entity Framework 4 search by combined fields

How to search in two combined fields. As necessary, the search should occur at the end of SQL.

So to speak, I have a customer table with a first and last name. I would like for users to be able to search on both of these columns with a single search box.

Currently my query 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)); 
+6
source share
2 answers

This is not possible if you did not specify a FullName column. The path around this problem could be String.Concat, which is resolved in Linq-to-entity:

 var query = DbContext.Customers .Where(p => String.Concat(p.FirstName, " ", p.LastName) .Contains(search)); 
+14
source

You can use the computed column in the database and display that for example.

 alter table Customer add FullName AS FirstName + ' ' + LastName 

(Don't know what I know)

+1
source

Source: https://habr.com/ru/post/886642/


All Articles