title: Optimizing a LINQ query with multiple joins and predicates
body: <p>Let's say that I have three tables in my SQL db - Users, Books, and UsersBooks.  </p>
  
  <p>I'm looking for ways to optimize the design/performance of a web method that takes a list of user names and returns a JSON object that contains users and their books.  I'm using Linq2Entities and SQL Server 2008.  </p>
  
  <p>Motivation for this question is that the web method is being called a lot (and growing!) so I need to find ways to make it run leaner and meaner.  </p>
  
  <p>Here's a stripped down version of the current design:</p>
  
  <pre><code>private string MyWebMethod(string input) {
      JavaScriptSerializer serializer = new JavaScriptSerializer();
      List&lt;string&gt; userNames = serializer.Deserialize&lt;List&lt;string&gt;&gt;(input);
  
      using(var db = new dbContext()) {
          var usersAndBooks = from u in db.Users
                              join ub in db.UsersBooks on u.UserId equals ub.UserId
                              join b in db.Books on ub.BookId equals b.BookId
                              select new Result { User = u, UsersBooks = ub, Book = b };
  
          var predicate = PredicateBuilder.False&lt;Result&gt;();
  
          // Construct predicate to filter usersAndBooks down to users we care about
          foreach(var name in userNames) {
              predicate = predicate.Or(r =&gt; r.User.UserName.Equals(name));
          }
  
          usersAndBooks = usersAndBooks.AsExpandable().Where(predicate);
  
          foreach(var name in userNames) {
              // Logic to construct final {User:{Books}} JSON object   
          }
      }
  }
  </code></pre>