title: How can I write a method to build a query both for SQL and an array?
body: <p>I have a method <code>BuildQuery</code> that dynamically generates a query. It works well for SQL:</p>
  
  <pre><code>    static MyType[] GetDataFromDB(MyDataContext db, string city, string district, string region, string country, string zip)
      {
          var q = BuildQuery(db.MyTable.AsQueryable(), city, district, region, country, zip);
          return q.ToArray();
      }
  
      private static IQueryable&lt;MyType&gt; BuildQuery(IQueryable&lt;MyType&gt; q, string city, string district, string region, string country, string zip)
      {
          if (!string.IsNullOrEmpty(city))
              q = q.Where(p =&gt; p.City.Contains(city));
          if (!string.IsNullOrEmpty(district))
              q = q.Where(p =&gt; p.District.Contains(district));
          if (!string.IsNullOrEmpty(zip))
              q = q.Where(p =&gt; p.Zip == zip);
          if (!string.IsNullOrEmpty(region))
              q = q.Where(p =&gt; p.Region.Contains(region));
          if (!string.IsNullOrEmpty(country))
              q = q.Where(p =&gt; p.Country == country);
          return q;
      }
  </code></pre>
  
  <p>(Actually, this query is a bit more compicated.) This works well building an SQL query with some <code>LIKE</code>.
  Now I would like to use the same query for an array of <code>MyType</code>:</p>
  
  <pre><code>    MyType[] SelectFromResult(MyType[] loc, string city, string district, string region, string country, string zip)
      {
          var q = BuildQuery(loc, city, district, region, country, zip);
          return q.ToArray();
      }
  </code></pre>
  
  <p>Of coarse this does not compile because <code>MyType[]</code> is an <code>IEnumerable&lt;MyType&gt;</code>, not an <code>IQueryable&lt;MyType&gt;</code>. Changing the type of the first argument in <code>BuildQuery</code> to <code>IEnumerable&lt;MyType&gt;</code>, compiles and works for arrays but won't build the SQL query.</p>
  
  <p>I guess I could make BuildQuery a generic method, but how? Any ideas?</p>