开发者

3 entities execute the same lambda predicate. How creating one lambda expression generic for these entities

开发者 https://www.devze.com 2023-01-22 12:08 出处:网络
I Have 3 entities. Product LangID ProductName Category LangID CatName ProductType LangID TypeName As you can see, each of them has LangID Property.

I Have 3 entities.

Product

LangID

ProductName

Category

LangID

CatName

ProductType

LangID

TypeName

As you can see, each of them has LangID Property. I Would like be able to create a generic repository 开发者_运维知识库that will contain only one function that will return an 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);
      }
   }

Someone has an idea ???.


One way you can accomplish this is by creating an interface with the LangID property, and have each of your classes implement this.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消