自定义类中的Lambda表达式

本文关键字:Lambda 表达式 自定义 | 更新日期: 2023-09-27 18:15:43

我不能使用这段代码,因为Member在UI中不可用。

public MemberViewModel GetSingle(Expression<Func<Member ,bool>> whereCondition ) 
{ 
    var member = this.MemberRepository.GetSingle( whereCondition ); 
    if (member != null) 
   { 
       return new MemberViewModel( member ); 
       // or however you map from member to its view model 
    } 
     return null; 
}

替换表达式的自定义类是什么样子的?它会像这样流动…

 public MemberViewModel GetSingle(CustomClass where) 
{ 
  // Create customExp of type Expression<Func<DAL.EntityModels.Category, 
  // bool>> out of CustomObject
  // _categoryRepository.GetSingle(customExp);
  // Convert the resulting dataobject to corresponding view object
  // return the view object
}

感谢您的帮助。

自定义类中的Lambda表达式

您是否可以使用类似的东西,然后从可以访问T(成员)和TViewModel (MemberViewModel)的地方调用它?

    public static TViewModel GetSingle<T, TViewModel, TRepository>(Expression<Func<T, bool>> whereCondition, TRepository repository)
        where T : class, Entity
        where TViewModel : class, IViewModel<T>, new()
        where TRepository : IRepository<T>
    {
        T member = repository.GetSingle(whereCondition);
        if (member != null)
        {
            return new TViewModel().MapFrom(member);
            // or however you map from member to its view model 
        }
        return null;
    }