我正在尝试向索引操作添加条件以仅获取某些记录。这个故事是这样的。
我到目前为止......
// GET: Assets
public ActionResult Index()
{
if(Roles.GetRolesForUser().Contains("Owner") == true)
{
// Get OwnerID from Owners table where Owners.RegUserID == User.Identity.GetUserId();
// Get list of assets from Asstes where Assets.OwnerID == OwnerID
}
else if(Roles.GetRolesForUser().Contains("Service Company") == true)
{
// Get ServiceCompanyID from Owners table where ServiceCompanies.RegUserID == User.Identity.GetUserId();
// Get list if assets from Assets where Asstes.ServiceCompanyID == ServiceCompanyID
}
return View(assets);
}
我完全不知道如何获取OwnerID或ServiceCompanyID,然后返回资产列表以返回视图。
答案 0 :(得分:0)
不确定您的数据库设置等。但根据您的问题,您应该能够做到这一点而不必担心什么"角色"用户使用的Linq查询类似于以下
var userId = User.Identity.GetUserId();
var asset = from a in context.Assets
join o in context.Owners on a.OwnerId equals o.OwnerId into oa
from asst in oa.DefaultIfEmpty()
join s in context.ServiceCompanies on a.ServiceCompanyId equals s.ServiceCompanyId into sa
from service in sa.DefaultIfEmpty()
where asst.RegUserId == userId || service.RegUserId == userId
select a;
return View(asset);