C# 如何在存储库模式中使用lambda连接表
我有一个存储库,它是:C# 如何在存储库模式中使用lambda连接表,c#,model-view-controller,C#,Model View Controller,我有一个存储库,它是: public class RepositoryTest<T>:IRepositoryTest<T> where T:class { private CentralEntities db = null; private DbSet<T> table = null; public RepositoryTest() { this.db = new CentralEntities(); tabl
public class RepositoryTest<T>:IRepositoryTest<T> where T:class
{
private CentralEntities db = null;
private DbSet<T> table = null;
public RepositoryTest() {
this.db = new CentralEntities();
table = db.Set<T>();
}
public RepositoryTest(CentralEntities db)
{
this.db = db;
table = db.Set<T>();
}
public IEnumerable<T> SelectAll(Expression<Func<T, bool>> predicate)
{
return table.Where(predicate).ToList();
}
}
公共类RepositoryTest:IRepositoryTest其中T:class
{
私有中心性db=null;
私有DbSet表=null;
公共存储测试(){
this.db=新的中心性();
table=db.Set();
}
公共存储测试(中央数据库)
{
这个.db=db;
table=db.Set();
}
公共IEnumerable SelectAll(表达式谓词)
{
返回表.Where(谓词).ToList();
}
}
其签名在以下界面中:
IEnumerable<T> SelectAll(Expression<Func<T, bool>> predicate);
IEnumerable SelectAll(表达式谓词);
在我的控制器中,我使用它来获取表的数据,它可以工作:
IRepositoryTest<UserRoles> _repository = null;
public DashbrdController(IRepositoryTest<UserRoles> _repository)
{
this._repository = _repository;
}
public DashbrdController()
{
this._repository = new RepositoryTest<UserRoles>();
}
public ActionResult DashBrd()
{
var rslt = _repository.SelectAll(s=>s.user_id==username);
return View(rslt);
}
IRepositoryTest\u repository=null;
公共DashbrdController(IRepositoryTest\u存储库)
{
此.\u repository=\u repository;
}
公共DashbrdController()
{
此._repository=new RepositoryTest();
}
公共行动结果DashBrd()
{
var rslt=\u repository.SelectAll(s=>s.user\u id==username);
返回视图(rslt);
}
现在的问题是,如果我想用用户id上的另一个表名it(courses)连接这个表,首先我需要在我的存储库?和控制器?中添加该类,然后如何连接它?你不能使用
Include
?@Haytam连接吗?问题是如何添加另一个表(类)从我的模型到这个控制器做加入或包括!?Include对类的属性起作用,因此您不需要将其他类添加到存储库中。您可以在这里制作示例吗?根据我的类?您可以添加您的类代码吗?和其他表(假设它们处于用户角色)。