title: How To Create Generic Data Access Object (DAO) CRUD Methods with LINQ to SQL
body: <p>I am new to LINQ to SQL and attempting to create a generic Data Access Object (DAO) for the basic Create, Read, Update, and Destroy (CRUD) methods so that I can reuse the code.  I was successful in creating a generic method that will delete any entity by using the code below but, I was wondering if anyone knows how to create a generic method that will select any entity by a common Id field that exists on all tables.</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Generic method that deletes an entity of any type using LINQ
      /// &lt;/summary&gt;
      /// &lt;param name="entity"&gt;&lt;/param&gt;
      /// &lt;returns&gt;bool indicating whether or not operation was successful&lt;/returns&gt;
      public bool deleteEntity(Object entity)
      {
          try
          {
              DomainClassesDataContext db = new DomainClassesDataContext();
              db.GetTable(entity.GetType()).Attach(entity);
              db.GetTable(entity.GetType()).DeleteOnSubmit(entity);
              db.SubmitChanges();
              return true;
          }
          catch(Exception ex)
          {
              Console.WriteLine(ex.StackTrace);
              return false;
          }
      }
  </code></pre>
  
  <p>I am pretty sure that the same patter will work for update and insert and would like to have a generic method on the GenericDAO that will retrieve me any entity (i.e. Customer, Invoice, WorkOrder, etc...) based on the entities Id.  Thanks in advance for the replies.</p>