2017-07-26 57 views
0

我正在學習UnitofWork,存儲庫模式,並在genericrepository文件中我有一個這樣的方法,我不知道如何在實際的例子中使用。加入兩個存儲庫模式與getwithinclude方法

/// <summary> 
/// Include multiple 
/// </summary> 
/// <param name="predicate"></param> 
/// <param name="include"></param> 
/// <returns></returns> 
public IQueryable<TEntity> GetWithInclude(System.Linq.Expressions.Expression<Func<TEntity, 
bool>> predicate, params string[] include) 
{ 
    IQueryable<TEntity> query = this.DbSet; 
    query = include.Aggregate(query, (current, inc) => current.Include(inc)); 
    return query.Where(predicate); 
} 

例如,如果我們有兩個倉庫REP1,REP2有外鍵關係將在連接兩個倉庫,並將其輸出到一個列表,我知道如何從單一的存儲庫的細節。

public IEnumerable<CREntity> GetAllCR() 
{ 
    var mapper = CreateMapper(); 
    var CRAll = _unitOfWork.Rep1.GetAll().ToList(); 
    //var joinFD = _unitOfWork.Rep1.GetWithInclude(); ---how to add Rep2 using getwithinclude 
    if (CRAll.Any()) 
    { 
     var CRModel = mapper.Map<List<ChangeRequest>, List<CREntity>>(CRAll); 
     return CRModel; 
    } 
    return null; 
} 
+0

看起來像所有你需要做的是'VAR數據= Rep1.GetWithInclude(X => x.ID == 5, 「ChildEntity1」 ,ChildEntity2);' – DavidG

+0

謝謝你的迴應,澄清一下是什麼是childentity1和childentity2 ...它們是實體類名稱Rep1和Rep2還是FK名稱? –

+0

'Include'必須包含類CREntity的導航屬性名稱,而不是存儲庫名稱。 –

回答

0

這是我的代碼示例:

public List<TblUser> GetUserListWithRoles() 
{ 
    GenericRepository<TblUser> repository = new GenericRepository<TblUser>(_context); 

    List<TblUser> userList = new List<TblUser>(); 
    string[] includes = { "TblUserRol", "TblUserRol.FkIdRolNavigation" }; 
    userList = repository.GetWithInclude(c => c.State == true, includes).ToList(); 

    return userList; 
}