我正在尝试在 MVC4 中合并来自两个存储库的数据。我在我的 Action 函数中使用 join 语句从两个表中收集信息。我正在将此数据传递到我的视图中。我对如何正确组合来自两个存储库的数据有点困惑。一些帮助将不胜感激。谢谢!
在控制器中:
public IOwnerRepository OwnerDB;
public IDwellingRepository DwellingDB;
public OwnerController() : this(new OwnerRepository(), new DwellingRepository()) {}
public OwnerController(IOwnerRepository oRepository, IDwellingRepository dRepository)
{
OwnerDB = oRepository;
DwellingDB = dRepository;
}
行动:
public ActionResult Account(int id, int? activelink)
{
// Link values
// Account = 0
// Listings = 1
// Profile = 2
// Create Property = 3
// Check for a value
// If no value set to zero
if (!activelink.HasValue)
activelink = 0;
// ####ing hackers
if (activelink > 3)
activelink = 0;
ViewBag.ActiveLink = activelink.ToString();
switch (activelink)
{
case 0:
ViewBag.Title = "Account Details";
break;
case 1:
ViewBag.Title = "Listings";
break;
case 2:
ViewBag.Title = "Profile";
break;
case 3:
ViewBag.Title = "Create Property";
break;
}
var oOwner = OwnerDB.FindOne(GetUserId(), id);
if (oOwner == null)
{
return new FileNotFoundResult { Message = "No Account found for id " + id.ToString() };
}
else
{
return View(oOwner);
}
}
所有者存储库:
public Owner FindOne(string UserId, int id)
{
var dwelling = (from o in db.Owner
join d in db.Dwelling on o.ID equals d.Owner_ID into ps
from k in ps.DefaultIfEmpty()
select o );
var oOwner = (from p in dwelling
where p.ID == id
select p);
return oOwner.FirstOrDefault();
}