79110467

Date: 2024-10-21 14:24:36
Score: 1.5
Natty:
Report link

As mentioned doing a "like" forces the database to scan, also the if statement complicates the query plan solution further

Can you rather select the data and do the contains on the app side?

var linkageData = query
    .SelectMany(p => p.AddressBookLinkage)
    .AsNoTracking()
    .Select(linkage => new 
    {
        MainContactId = linkage.MainContactId,
        Name = linkage.MainContactId == null ? linkage.MainSubject.Name : linkage.MainContact.Name
    })
    .ToList();

var result = linkageData
    .Where(linkage => linkage.Name.Contains(searchedTerm))
    .ToList();
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (0.5):
Posted by: Ockert