title: How to return IQueryable from an SQL expression?
body: <p>I am getting the following error trying to query from my database using C#:</p>
  
  <p><strong><em>LINQ to Entities does not recognize the method 'MyProject.Models.DB.Department getDepartment()' method, and this method cannot be translated into a store expression.</em></strong></p>
  
  <p>In my controller I have:</p>
  
  <pre><code>var query = db.Activities.AsQueryable();
  query = query.Where(x =&gt; rs.department.Contains(x.getDepartment().id));
  </code></pre>
  
  <p>In my model I have the following method which returns the correct <code>Department</code> for an <code>Activity</code>. I know the problem is that I am executing the query when I do the <code>.First()</code>, but I can't find a way to return this as a <code>IQueryable</code> result:</p>
  
  <pre><code>public partial class Activity
  {
     public Department getDepartment()
      {
          return db.Departments.SqlQuery( "" +
              "SELECT * FROM " +
                  "[snt].[Departments] " +
              "INNER JOIN( " +
                  "SELECT " +
                      "[Users].departments_id AS id " +
                  "FROM " +
                      "[snt].[Users], " +
                      "[snt].[People], " +
                      "[snt].[EventPerson], " +
                      "[snt].[EventPersonTask], " +
                      "[snt].[Tasks], " +
                      "[snt].[Events], " +
                      "[snt].[Activities] " +
                  "WHERE " +
                      "[snt].[Activities].id = {0} AND " +
                      "[snt].[Events].id = [snt].[Activities].events_id AND " +
                      "[snt].[EventPerson].events_id = [snt].[Events].id AND " +
                      "[snt].[EventPerson].people_id = [snt].[People].id AND " +
                      "[snt].[EventPersonTask].tasks_id = [snt].[Tasks].id AND " +
                      "[snt].[EventPersonTask].eventperson_id = [snt].[EventPerson].id AND " +
                      "[snt].[Tasks].name = 'owner' AND " +
                      "[snt].[Users].is_active = 1 AND " +
                      "[snt].[Users].people_id = [snt].[People].id" +
              " ) AS x " +
              "ON " +
                  "x.id = [Departments].id", this.id).First();
      }
  }
  </code></pre>