LINQ query using StartsWith and Contains to limit the results for an autocomplete field As a source for a jquery-ui autocomplete field, I've written this action:

public ActionResult Autocomplete(string term)
{
    List&lt;Relation&gt; relations =
        db.Relation
            .Where(l =&gt; l.AdministrationId == Helpers.Session.AdministrationId &amp;&amp;
                        l.IsSupplier == true)
            .ToList();

    term = term.ToLowerInvariant();

    List&lt;string&gt; results = new List&lt;string&gt;();

    foreach (Relation r in relations) {
        string name = r.Name.ToLowerInvariant();

        if (name.StartsWith(term) || name.Contains(term)) {
            results.Add(name);
        }
    }

    results = results.OrderBy(l =&gt; l).Take(5).ToList();

    return Json(results, JsonRequestBehavior.AllowGet);
}


Since this is very verbose and probably far from optimized, I'd like to know what I can improve.

I've tried to put it all in a single LINQ query, like the code below. However, LINQ will try to interpret everything as extension methods and throw an error (ToLowerInvariant() and StartsWith()).

term = term.ToLowerInvariant();

IEnumerable&lt;string&gt; suppliers =
    db.Relations
        .Where(l =&gt; l.AdministrationId == Helpers.Session.AdministrationId &amp;&amp;
                    l.IsSupplier== true &amp;&amp;
                    (l.Name.ToLowerInvariant().StartsWith(term) ||
                     l.Name.ToLowerInvariant().Contains(term)))
        .Select(l =&gt; l.Name)
        .OrderBy(l =&gt; l)
        .Take(5);


What I'm looking for is a way to improve the first code snippet. It looks very verbose now and I'm pretty sure it's far from optimized when handling a lot of requests and when I have lots of rows in my database.
Naming
Instead of term maybe searchTerm would be better. As in your second example you name the IEnumerable&lt;&gt; variable suppliers, your method would be better named as AutocompleteSuppliers.  

Review  

A Boolean variable can be evaluated just by using the variable, so &amp;&amp; l.IsSupplier == true can be replaced by &amp;&amp; l.IsSupplier. As an evaluation of an boolean is faster as the comparing of an int (assuming AdministrationId  is an int) we change the order of the where condition like  

List&lt;Relation&gt; relations =
    db.Relation
        .Where(l =&gt; l.IsSupplier &amp;&amp;
               l.AdministrationId == Helpers.Session.AdministrationId)  


Inside the foreach loop you are first checking if the Relation.Name is starting with term and if not, you are checking if term is contained inside Relation.Name. This will be faster if term is often the starting of the name, but will be slower if it is contained as of the double check.
So let us just remove the StartsWith().  

Your combination of the Linq statement and the loop does look ok, but will produce different results than in the Autocomplete() method, as you are selecting Relation.Name instead of Relation.Name.ToLowerInvariant().

Refactoring  

As this refactoring is based on the original method we use the Select() with ToLowerInvariant() first and are checking afterwards if it contains the searchTerm  

public ActionResult AutocompleteSuppliers(string searchTerm)
{
    searchTerm = searchTerm.ToLowerInvariant();

    IEnumerable&lt;string&gt; suppliers = db.Relations
        .Where(l =&gt; l.IsSupplier &amp;&amp; l.AdministrationId == Helpers.Session.AdministrationId)
        .Select(l =&gt; l.Name.ToLowerInvariant())
        .Where(s =&gt; s.Contains(searchTerm))
        .OrderBy(s =&gt; s)
        .Take(5);

    return Json(suppliers, JsonRequestBehavior.AllowGet);

}