title: querying a collection against a byte property in mongodb with c# driver
body: <p>I am fairly new to mongodb and I am trying to query a collection with byte property but I am getting an invalid cast exception. To summarize,</p>
  
  <pre><code>public class MongoDbAddressCollection    
  {
       private IMongoCollection&lt;Addresses&gt; collection = database.GetCollection&lt;Addresses&gt;("AddressCollection");
       public IEnumerable&lt;Addresses&gt; Where(System.Linq.Expressions.Expression&lt;Func&lt;Addresses, bool&gt;&gt; filter = null)
       {
           var items = collection.AsQueryable().Where(filter);
           if (items == null)
               return null;
           return items.ToList();
       }
  }
  
  public class Test
  {
      public void DoSomthingWithAddresses()
      {
           MongoDbAddressCollection addressCollection=new MongoDbAddressCollection();
           //exception occurs in MongoDbAddressCollection.Where method when executing the ToList().
           List&lt;Addresses&gt; homeAddresses=addressCollection.Where(x=&gt;x.AddressType==(byte)EnumAddressType.HomeAddress);
           foreach(var address in homeAddresses)
           {
              //do stuff
           }
      }
  }
  </code></pre>
  
  <p>And the address entity is as following class</p>
  
  <pre><code>public class Address
  {
      public Guid UserId { get; set; }
      public string Street { get; set; }
      public byte AddressType { get; set; }
      .....
  }
  
  public enum EnumAddressType
  {
      HomeAddress=1,
      WorkAddress=2
  }
  </code></pre>
  
  <p>Actually the issue seems to be a type mismatch between mongodb and .net, I can see that the AddressType for WorkAddress entities is 10 which is binary representation of 2. But, I cannot think of a solution other than changing all byte properties in all of our entities to int before I insert them to collections (we have lots of byte properties in different entities)... Or can this be fixed with a workaround in the query (linq where condition)?</p>
  
  <p>Thanks in advanced.</p>