title: Querying POCO Entities from DocumentDB
body: <p>I am following <a href="https://azure.microsoft.com/tr-tr/documentation/articles/documentdb-dotnet-application/" rel="nofollow">this blog post</a> from Microsoft testing out DocumentDB.</p>
  
  <p>I have created a collection and inserted 2 documents via different POCO classes on my application. It created the documents but I cannot filter them back into their respective POCO classes.  I realized that I am querying all the collection so it is obviously retrieving all the documents stored inside that collection.</p>
  
  <p>What is the best way to differentiate documents while querying so that I can query them separately by type?</p>
  
  <p>I can add a type field to the document and can get by <code>WHERE type="user"</code> but I'm not sure that I cannot do <code>SELECT * FROM users</code> with <code>users</code> being a document type (if there is such a thing in DocumentDB), not a collection.</p>
  
  <p>Here is how I am creating documents:</p>
  
  <pre><code>    var user1= new User()
      {
          UserTypeId = 0,
          UserName = "user1@hotmail.com",
          Password = "12345",
          PasswordSalt = "saltyPassword",
          UserStatusId = 1,
          ProfilePhotoKey = "KJSY"
      };
      await DocumentDBRepository&lt;User&gt;.CreateItemAsync(user1);
  
      var client = new Client()
      {
          ClientName = "client1",
          Secret = "rxPBsIVYya2Jg2ZHPNG8gL0P36TnutiBehvEFgk938M=",
          Title = "Administration Front End Application",
          ApplicationTypeId = 0,
          Active = false,
          RefreshTokenLifeTime = 60,
          AllowedOrigin = "http://localhost:8080",
          AllowedRoles = "admin"
      };
      await DocumentDBRepository&lt;Client&gt;.CreateItemAsync(client);
  </code></pre>
  
  <p>Document Db repository class</p>
  
  <pre><code>public static class DocumentDBRepository&lt;T&gt;
  {
      //Use the Database if it exists, if not create a new Database
      private static Database ReadOrCreateDatabase()
      {
          var db = Client.CreateDatabaseQuery()
                          .Where(d =&gt; d.Id == DatabaseId)
                          .AsEnumerable()
                          .FirstOrDefault();
  
          if (db == null)
          {
              db = Client.CreateDatabaseAsync(new Database { Id = DatabaseId }).Result;
          }
  
          return db;
      }
  
      //Use the DocumentCollection if it exists, if not create a new Collection
      private static DocumentCollection ReadOrCreateCollection(string databaseLink)
      {
          var col = Client.CreateDocumentCollectionQuery(databaseLink)
                            .Where(c =&gt; c.Id == CollectionId)
                            .AsEnumerable()
                            .FirstOrDefault();
  
          if (col == null)
          {
              var collectionSpec = new DocumentCollection { Id = CollectionId };
              var requestOptions = new RequestOptions { OfferType = "S1" };
  
              col = Client.CreateDocumentCollectionAsync(databaseLink, collectionSpec, requestOptions).Result;
          }
  
          return col;
      }
  
      //Expose the "database" value from configuration as a property for internal use
      private static string databaseId;
      private static String DatabaseId
      {
          get
          {
              if (string.IsNullOrEmpty(databaseId))
              {
                  databaseId = ConfigurationManager.AppSettings["database"];
              }
  
              return databaseId;
          }
      }
  
      //Expose the "collection" value from configuration as a property for internal use
      private static string collectionId;
      private static String CollectionId
      {
          get
          {
              if (string.IsNullOrEmpty(collectionId))
              {
                  collectionId = ConfigurationManager.AppSettings["collection"];
              }
  
              return collectionId;
          }
      }
  
      //Use the ReadOrCreateDatabase function to get a reference to the database.
      private static Database database;
      private static Database Database
      {
          get
          {
              if (database == null)
              {
                  database = ReadOrCreateDatabase();
              }
  
              return database;
          }
      }
  
      //Use the ReadOrCreateCollection function to get a reference to the collection.
      private static DocumentCollection collection;
      private static DocumentCollection Collection
      {
          get
          {
              if (collection == null)
              {
                  collection = ReadOrCreateCollection(Database.SelfLink);
              }
  
              return collection;
          }
      }
  
      //This property establishes a new connection to DocumentDB the first time it is used, 
      //and then reuses this instance for the duration of the application avoiding the
      //overhead of instantiating a new instance of DocumentClient with each request
      private static DocumentClient client;
      private static DocumentClient Client
      {
          get
          {
              // change policy to ConnectionMode: Direct and ConnectionProtocol: TCP on publishing to AZURE
              if (client == null)
              {
                  string endpoint = ConfigurationManager.AppSettings["endpoint"];
                  string authKey = ConfigurationManager.AppSettings["authKey"];
                  Uri endpointUri = new Uri(endpoint);
                  client = new DocumentClient(endpointUri, authKey);
              }
  
              return client;
          }
      }
  
  
      /* QUERY HELPERS */
      public static IEnumerable&lt;T&gt; GetAllItems()
      {
          return Client.CreateDocumentQuery&lt;T&gt;(Collection.DocumentsLink)
              .AsEnumerable();
      }
      public static IEnumerable&lt;T&gt; GetItems(Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
      {
          return Client.CreateDocumentQuery&lt;T&gt;(Collection.DocumentsLink)
              .Where(predicate)
              .AsEnumerable();
      }
      public static async Task&lt;Document&gt; CreateItemAsync(T item)
      {
          return await Client.CreateDocumentAsync(Collection.SelfLink, item);
      }
      public static T GetItem(Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
      {
          return Client.CreateDocumentQuery&lt;T&gt;(Collection.DocumentsLink)
                      .Where(predicate)
                      .AsEnumerable()
                      .FirstOrDefault();
      }
  
      public static async Task&lt;Document&gt; UpdateItemAsync(string id, T item)
      {
          Document doc = GetDocument(id);
          return await Client.ReplaceDocumentAsync(doc.SelfLink, item);
      }
  
      private static Document GetDocument(string id)
      {
          return Client.CreateDocumentQuery(Collection.DocumentsLink)
              .Where(d =&gt; d.Id == id)
              .AsEnumerable()
              .FirstOrDefault();
      }
  }
  </code></pre>
  
  <p>I am trying to get:</p>
  
  <pre><code>    var q = DocumentDBRepository&lt;User&gt;.GetAllItems().ToList();
      var t = DocumentDBRepository&lt;Client&gt;.GetAllItems().ToList();
  </code></pre>
  
  <p>q should contain only user documents those were created by </p>
  
  <pre><code>await DocumentDBRepository&lt;User&gt;.CreateItemAsync(user1);
  </code></pre>
  
  <p>and t should contain only client documents those were created by</p>
  
  <pre><code>await DocumentDBRepository&lt;Client&gt;.CreateItemAsync(client1);
  </code></pre>