Accepted answer

Yes. You could use the curiously recurring template pattern, define EntityTable : CustomBaseTable<EntityTable>, and, thus, have EntityTable available as a generic parameter. Here is a minimal example (fiddle):

using System;
using System.Linq.Expressions;
public class Program
    public static void Main()
        var Value1 = "value1";
        var z_objEntity = new EntityTable();
        z_objEntity.CustomWhere1(t => t.Field1 == Value1); // compiles!
    public class CustomBaseTable<T>
         public void CustomWhere1(Expression<Func<T, bool>> p_expWhereClause)
             throw new NotImplementedException();
    public class EntityTable : CustomBaseTable<EntityTable>
        public string Field1 { get; set; }

That having been said, I do believe that the extension class is the simplest solution for your problem.

CustomWhere3 neatly avoid both flaws, but apparently requires creating a separate extension class. I'll go with it if I need to, but I fail to see why it's needed.

It's needed because you

  • want to define CustomWhere in your base class,
  • CustomWhere has a parameter whose type depends on the concrete derived class, and
  • C# does not (yet) have a "this" type.

Thus, we either need to

  • make the derived class available to the base class (which is what the code in my answer does) or
  • define CustomWhere somewhere else (which is what your extension method example CustomWhere3 does).

Related Articles