title: Customizing IQueryable<T>
body: <p>I'm trying to customize entities of my application to make them have a property referencing the DataContext that loaded them.</p>
  
  <p>I think the best way is to somehow create a class that implements the IQueryable and set the entity datacontext property in its GetEnumerator method.</p>
  
  <p>My question is, how can I use the Provider and Expression used by Linq to SQL in my implementation of IQueryable so that I don't have to implement them myself?</p>
  
  <p>BTW: For my scenario, is there another way?</p>
  
  <p>Take a look at the following code:</p>
  
  <pre><code>public partial class Product: IEntityBase  
  { 
  
      public Product() 
      { 
          _DataContext = new SampleDataContext(); 
      } 
  
      private long _Id;  
      [Column(Storage="_Id", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]  
      public long Id  
      {  
           get{ return _Id; }  
           set{ _Id = value; }  
      }  
  
      private string _Name;  
      [Column(Storage="_Name", DbType="NVarChar(MAX) NOT NULL", CanBeNull=false  
      public string Name  
      {  
          get{ return _Name; }  
          set{ _Name = value; }  
      }  
  
      private SampleDataContext _DataContext;  
  
      //This is the property extending the Product class and should be set when this class is being returned 
      //by IQueryable&lt;T&gt;.GetEnumerator() 
      public SampleDataContext DataContext  
      {  
          get{ return _Name; }  
          set{ _Name = value; }  
      }  
  
      public MyQueryable&lt;Product&gt; GetProducts() 
      { 
          MyQueryable&lt;Product&gt; result = from p in context.Products 
                                        where {Some Conditions 1} 
                                        select p; 
          result.DataContext = _DataContext; 
          return result; 
      } 
  
      public void SomeMethod() 
      { 
          //This query will not actually set the DataCotnext property. 
          //And the generated sql query is something like:  
          //SELECT * FROM Products WHERE {Some Conditions 1} AND {Some Conditions 2} 
          var products = GetProducts().Where( {Some Conditions 2} ); 
  
          //Now that the GetEnumerator() is called the DataContext property of the products 
          //will be set. 
          foreach( var item in products ) 
          { 
              item.Name = "Test Name"; 
              item.DataContext.SubmitChanges(); 
          } 
      } 
  }  
  
  public MyQueryable&lt;T&gt;: IQueryable&lt;T&gt; 
      where T: class, IEntityBase 
  { 
      // 
      //Implementation of IQueryable which is my question 
     // 
  
     public IEnumerator&lt;T&gt; GetEnumerator() 
     { 
         foreach( var item in Provider.GetEnumerator&lt;T&gt;() ) 
         { 
              item.DataContext = this.DataContext; 
              yield return item; 
         } 
     } 
  
     public SampleDataContext DataContext{ get; set; } 
  } 
  
  public interface IEntityBase 
  { 
      SampleDataContext DataContext{ get; set; }; 
  } 
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I found the answer myself. Here it is the sample code to show how I did that.</p>
  
  <pre><code>public MyQueryable&lt;T, TContext&gt;: IQueryable&lt;T&gt; 
      where T: class, IEntityBase 
      where TContext: DataContext, new()
  { 
  
     public MyQueryable&lt;T&gt;(TContext context, IQueryable&lt;T&gt; baseIQueryable)
     {
          if( baseIQueryable == null )
              throw new ArgumentNullException("baseIQueryable");
  
          this.Provider = baseIQueryable.Provider;            
          this.Expression = baseIQueryable.Expression;
  
          this.DataContext = context;
     }
  
     public IEnumerator&lt;T&gt; GetEnumerator()
     {
         var enumerator = Provider.Execute&lt;IEnumerable&lt;T&gt;&gt;(Expression);
         foreach( var item in enumerator )
         {
             item.DataContext = this.DataContext ?? new TContext();
             yield return item;
         }
     }
  
     IEnumerator IEnumerable.GetEnumerator()
     {
         var enumerator = Provider.Execute&lt;IEnumerable&gt;(Expression);
         foreach( var item in enumerator )
         {
             ((IEntityBase&lt;TContext&gt;)item).DataContext = this.DataContext;
             yield return item;
         }
     } 
  
     //
     //Other implementations...
     //
     public SampleDataContext DataContext{ get; set; } 
  } 
  
  public partial class Product: IEntityBase
  {
      public MyQueryable&lt;Product&gt; GetProducts() 
      { 
          var result = from p in context.Products 
                       where {Some Conditions 1} 
                       select p; 
          return new MyQueryable&lt;typeof(Product), DataContext&gt;(this.DataContext, result);
      }         
  }
  </code></pre>