基于用户角色/表返回表记录加入失败 - linq查询

时间:2016-06-22 01:32:33

标签: asp.net-mvc linq asp.net-identity

我正在构建一个MVC 5(EF6)应用程序,该应用程序具有链接到users表的摄影师表。

我通过在用户帐户上设置角色来识别摄影师。摄影师表保存附加信息而非表格。我有不同类型的用户访问系统。

我需要根据用户处于摄影师角色来获取摄影师桌面记录。我有以下解决方案,但当我尝试访问查询结果时,它返回以下错误消息。

  

无法创建类型' Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole'的常量值。在此上下文中仅支持原始类型或枚举类型。

var photogpraherRole = db.Roles.Single(r => r.Name == "photographer");
var users = photogpraherRole.Users.ToList();

var photographersList =
    from photographer in db.Photographers
    join user in users on photographer.User.Id equals user.UserId
    orderby photographer.Priority
    select photographer;

foreach (var photographer in photographersList)
{
    var photographerName = photographer.User.FirstName;
}

1 个答案:

答案 0 :(得分:0)

经过一些研究和@Shawn Yan的帮助,这是我提出的解决方案。

var photographerRole = db.Roles.SingleOrDefault(m => m.Name == "photographer");
var disabledRole = db.Roles.SingleOrDefault(m => m.Name == "disabled");

var users = db.Users.Where(m => m.Roles.All(r => r.RoleId == photographerRole.Id && r.RoleId != disabledRole.Id)).ToList();

var photographersList =
    from photographer in db.Photographers.ToList()
    join user in users on photographer.User.Id equals user.Id
    where photographer.Location == booking.location.Location
    orderby photographer.Priority
    select photographer;
var photographers = photographersList.ToList();