实体框架联盟和不同类型的查询除外

时间:2012-03-24 15:26:28

标签: c# .net linq entity-framework entity-framework-4

我希望用户实体具有Site角色的Action但是点是ExtraAction实体,Action数据将由ExtraAction实体过滤,

在ExtraAction实体中

如果Type属性== 1这将是UNION to Action实体
 如果Type属性== 0,则此为EXCEPT到Action实体

  public class User
    {
        public int Id { get; set; }
        public string Email { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        public ICollection<SiteRole> SiteRoles { get; set; }
        public ICollection<ExtraAction> ExtraActions { get; set; }
    }

    public class SiteRole
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public virtual ICollection<Action> Actions { get; set; }
        public virtual ICollection<User> User { get; set; }
    }

    public class ExtraAction
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public int ActionId { get; set; }
        public byte Type { get; set; }

        public virtual Action Action { get; set; }
        public virtual User User { get; set; }
    }

    public class Action
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string ActionName { get; set; }
        public string ControllerName { get; set; }
        public ICollection<SiteRole> SiteRoles { get; set; }
        public virtual ICollection<ExtraAction> ExtraActions { get; set; }

    }

1 个答案:

答案 0 :(得分:1)

最后我的解决方案在

之下
 var list = dbContext.Actions.Where(u =>
                u.Roles.SelectMany(r => r.User).Any(su => su.Id == Id)).Select(row => new { Action = row }).
                Union(dbContext.ExtraActions.Where(suea => suea.Type == 1 && suea.UserId == Id).Select(row => new { Action = row.Action })).
                Except(dbContext.ExtraActions.Where(suea => suea.Type == 0 && suea.UserId == Id).Select(row => new { Action = row.Action })).ToList();