title: Performance Issue with Linq query while checking filter text in Contains method
body: <p>I m trying to execute the below Linq Query , but the thing is when i'm executing below Linq Query with the Search query text , im getting sql timeout all the times. please suggest better way to optimize this . My Db table has nearly half million records.</p>
  
  <pre><code>{
              QueryBuilder = (from ub in idamDbContext.UserBasicInfo
                              join ur in idamDbContext.UserRoles on ub.UserId equals ur.UserBasicInfoId
                              join r in idamDbContext.Role on ur.RoleId equals r.Id
                              join urt in idamDbContext.UserType on r.UserTypeId equals urt.Id
                              where (r.UserTypeId == 1 || r.UserTypeId == 3 || r.UserTypeId == 2) &amp;&amp;
                              (territoryId == null || ub.TerritoryId == territoryId) &amp;&amp;
                              (centerId == null || ub.CenterId == centerId) &amp;&amp;
                              (isActive == null || ub.IsActive == isActive)
                              orderby ub.Created
                              select new UserProfileDTO
                              {
                                  UserId = ub.UserId,
                                  UserName = ub.UserName,
                                  FirstName = ub.FirstName,
                                  LastName = ub.LastName,
                                  Email = ub.Email,
                                  TerritoryId = ub.TerritoryId,
                                  UserTypeId = r.UserTypeId,
                                  Description = r.Description,
                                  Name = urt.Name
                              }
                     );
  
              if (!string.IsNullOrEmpty(query))
                  QueryBuilder = QueryBuilder.Where(x =&gt; (!string.IsNullOrEmpty(x.FirstName) &amp;&amp; x.FirstName.ToLower().Contains(query)) ||
                  (!string.IsNullOrEmpty(x.LastName) &amp;&amp; x.LastName.ToLower().Contains(query)) ||
                  (!string.IsNullOrEmpty(x.Email) &amp;&amp; x.Email.ToLower().Contains(query)) ||
                  (!string.IsNullOrEmpty(x.Description) &amp;&amp; x.Description.ToLower().Contains(query)) ||
                  (!string.IsNullOrEmpty(x.UserName) &amp;&amp; x.UserName.ToLower().Contains(query)) ||
                  (!string.IsNullOrEmpty(x.Name) &amp;&amp; x.Name.ToLower().Contains(query))
                  );
          }
  </code></pre>