A generic Find method to search by Guid type for class implementing IDbSet interface
        Posted  
        
            by 
                imak
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by imak
        
        
        
        Published on 2012-10-08T03:36:11Z
        Indexed on 
            2012/10/08
            3:36 UTC
        
        
        Read the original article
        Hit count: 170
        
I am implementing a FakeDataSet class by implementing IDbSet interface. As part of implementing this interface, I have to implement Find method. All my entity classes has an Guid type Id column. I am trying to implement Find method for this FakeDbSet class but having hard time to write it in a generic way. Below is my attempts for writing this method but since it does not know about Id been Guid type, I am getting compilation error on m.Id call. Any ideas on how this could be accomplished?
public class FakeDataSet<T> : IDbSet<T> where T: class, new()  
{  
  //  Other methods for implementing  IDbSet interface
  public T Find(params object[] keyValues)  
  {  
       var keyValue = (Guid)keyValues.FirstOrDefault();
       return this.SingleOrDefault(m => m.Id == keyValue);  // How can I write this
  }
}
© Stack Overflow or respective owner