How to query with Contains on a concatenated string in Entity Framework

Viewed 498

My Serial object contains the following values

string Class
int Product
int Code

Together they form a Serial Number. Note that 2 of them are integers.

Class + Product.ToString() + "-" + Code.ToString()

I am attempting to filter based on a string. Example:

serials = _context.Serials.AsQueryable();

serials = serials.Where(serial => serial.Class.Contains(searchText));

I am looking to perform a .Contains() on the concatenated SerialNumber. How would I perform this in EF Core?

Something like this does not work. Throwing an error about query not being translated.

serials = serials.Where(serial => string.Concat(serial.ProductClass, serial.ProductNumber.ToString(), serial.CodeNumber.ToString()).Contains(searchText))
1 Answers

Only string.Concat(string, string) overload is supported.

Use + operator instead:

.Where(serial => 
    (serial.ProductClass + serial.ProductNumber.ToString() + serial.CodeNumber.ToString())
    .Contains(searchText))
Related