EF Core 2.1 has added support for FREETEXT
, as also discussed in How to use FreeText in EF core 2.1. I have a different problem, however, using EF Core 2.2: Does EF Core's FREETEXT
support child entities?
public class Customer
{
public Name Name { get; set; }
public List<Address> Addresses { get; set; }
}
Name is an owned entity (value object), which works perfectly for searching:
public class Name
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Address is a child entity:
public class Address
{
public string Street { get; set; }
public string Number { get; set; }
}
This search works fine:
query.Where(c => EF.Functions.Freetext(c.Name.FirstName, searchTerm) || EF.Functions.Freetext(c.Name.LastName, searchTerm)
This search does not, as the final term cannot be translated to SQL:
query.Where(c => EF.Functions.Freetext(c.Name.FirstName, searchTerm) || EF.Functions.Freetext(c.Name.LastName, searchTerm) || EF.Functions.Freetext(c.Addresses.First().Street, searchTerm)
Is there any way around this, or will I need to use a SQL function? I've tried using a Select()
statement, but that could also not be fully translated to SQL.
Found it! Apparently, EF.Functions.FreeText(c.Addresses.First().Street, searchTerm)
cannot be evaluated client-side. However, this can:
EF.Functions.FreeText(c.Addresses.Any(a => EF.Functions.FreeText(a.Street, searchTerm))
So make sure EF.Functions.FreeText()
receives a simple string
as its first property, and use any other LINQ for selecting the First()
, 'Last()', Any()
and All()
of child entities.