代码之家  ›  专栏  ›  技术社区  ›  Jean-Francois

3个实体执行相同的lambda谓词。如何为这些实体创建一个lambda表达式泛型

  •  0
  • Jean-Francois  · 技术社区  · 14 年前

    我有三个实体。
    产品
    兰吉德


    类别
    兰吉德
    猫名

    商品种类表

    类型名

    如您所见,它们每个都有LangID属性。 Func<T, bool> GetLmbLang()

       public interface IBaseRepository<T> where T : class
       {
            Func<T, bool> GetLmbLang();
       }
    
       public class BaseRepository<T> : IBaseRepository<T> where T : class
       {
    
          public Func<T, bool> GetLmbLang()
          {
              //ERROR HERE
              //That dosen't work here
              return (p => p.LangID == 1);
          }
       }
    

    有人有主意???。

    1 回复  |  直到 14 年前
        1
  •  1
  •   jordanbtucker    14 年前

    实现这一点的一种方法是创建一个带有LangID属性的接口,并让每个类实现它。

    public interface IHasLangID
    {
      string LangID { get; set; }
    }
    
    public class Product : IHasLangID
    ...
    public class Category : IHasLangID
    ...
    public class ProductType : IHasLangID
    ...
    
    public interface IBaseRepository<T> where T : IHasLangID
    ...
    public class BaseRepository<T> : IBaseRepository<T> where T : IHasLangID