2011-11-26 87 views
0

我無法使用此代碼,因爲成員在UI中不可用。自定義類中的Lambda表達式

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 
} 

任何幫助表示讚賞。

+0

哪種語言? Java的? C#? Scala呢? –

+1

@Anonymouse:表達式是C#:) – leppie

+0

具體的.Net 4.0 –

回答

0

您可以使用類似於此的東西,然後從可以訪問T(Member)和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; 
    } 
+0

我試圖不讓調用應用程序訪問會員只是MemberView。我決定不走這條路,回到簡單的參數。 –