【问题标题】:Entity Framework Union and Except Query On Different TypesEntity Framework Union 和 except 不同类型的查询
【发布时间】:2012-03-24 15:26:28
【问题描述】:

我想使用站点角色的操作的用户实体,但点是ExtraAction 实体,操作数据将由ExtraAction 实体过滤,

在ExtraAction 实体中:

如果 Type 属性 == 1 这将是 UNION 到 Action 实体
if Type property == 0 this to be EXCEPT to Action entity

  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; }

    }

【问题讨论】:

  • 我不明白你想表达什么?你能澄清一下你的实际问题是什么吗?
  • 显示你的 linq 语句应该澄清很多。
  • 我做到了,但很难解释有点抱歉。

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


【解决方案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();

【讨论】:

    猜你喜欢
    • 2012-01-23
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    相关资源
    最近更新 更多