Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 15th, 2012  |  syntax: None  |  size: 1.62 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Assign an object to the class from within
  2. public partial class Table1
  3. {
  4. public Table1(int id)
  5. {
  6.     using (DBDataContext item = new DBDataContext())
  7.         {
  8.             var q = (from c in item.table1
  9.                      where c.ID == id
  10.                      select c).FirstOrDefault();
  11.         }
  12. }
  13. }
  14.        
  15. public static Table1 TableFromId(int Id)
  16. {
  17.      using (DBDataContext item = new DBDataContext())
  18.         {
  19.             return  (from c in item.table1
  20.                      where c.ID == id
  21.                      select c).FirstOrDefault() as Table1;
  22.         }
  23. }
  24.        
  25. Table1 table = Table1.TableFromId(0);
  26.        
  27. public class Person: ICopyable<Person>
  28. {
  29.     public string Name {set; get;}
  30.     public int Age {set; get;}
  31.  
  32.     public void  CopyFrom(Person otherObject)
  33.     {
  34.         if (otherObject != null)
  35.         {
  36.             Name = otherObject.Name;
  37.             Age = otherObject.Age;
  38.         }
  39.     }
  40.  
  41.     public Table1(int id)
  42.     {
  43.         using (DBDataContext item = new DBDataContext())
  44.         {
  45.             var q = (from c in item.table1
  46.                  where c.ID == id
  47.                  select c).FirstOrDefault() as Person;
  48.             if (q != null)
  49.             {
  50.                 CopyFrom(q);
  51.             }
  52.         }
  53.     }
  54. }
  55.  
  56. public interface ICopyable<TType>
  57. {
  58.     void CopyFrom(TType otherObject);
  59. }
  60.        
  61. public partial class Table1
  62. {
  63.      public static FromID(int id)
  64.      {
  65.          using (var context = new DBDataContext()) {
  66.             return (Table1)(from c in context.table1
  67.                             where c.ID == id
  68.                             select c).FirstOrDefault();
  69.          }
  70.      }
  71. }
  72.        
  73. Table1 obj = Table1.FromID(17);