自定义类中的Lambda表达式

时间:2011-11-26 13:18:30

标签: c# .net lambda

我无法使用此代码,因为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
}

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

您是否可以使用与此类似的内容,然后从可以访问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;
    }