【问题标题】:LINQ to Create / Filter Object from Two Properties in HashSetLINQ 从 HashSet 中的两个属性创建/过滤对象
【发布时间】:2012-03-27 14:14:57
【问题描述】:

有三个类用户、组和字段。用户/组上的多对多关系和组字段上的多对多关系。用户/组关系非常简单,因为用户是否只是组的成员。组/字段关系更复杂,因为该关系上有一个权威属性。

在 ctor 中,我传递了关系的 HashSet,因此只有一个主控。

最后是问题。如何更好地过滤到 Group FieldAuthority?看 ??在下面的代码中。

    public class Group : Object
    {
        private HashSet<UserGroup> usersGroups;
        private HashSet<GroupFieldAuthority> groupsFieldsAuthority;

        public Int16 ID { get; private set; }
        public string Name { get; set; }

        public List<User> Users
        { get { return usersGroups.Where(x => x.Group == this).Select(x => x.User).OrderBy(x => x.UserID).ToList(); } }
        public List<FieldAuthority> FieldsAuthority
        {     
            get
            {
                // Can this be done more directly??
                List<FieldAuthority> fieldsAuthority = new List<FieldAuthority>();
                foreach (GroupFieldAuthority gfa in groupsFieldsAuthority.Where(x => x.Group == this).OrderBy(x => x.Group.Name))
                {
                    fieldsAuthority.Add(new FieldAuthority(gfa.FieldDef, gfa.Authority));
                }
                return fieldsAuthority;
            }
        }
        public override bool Equals(Object obj)
        {
            //Check for null and compare run-time types.
            if (obj == null || GetType() != obj.GetType()) return false;
            Group fd = (Group)obj;
            return (ID == fd.ID);
        }
        public override int GetHashCode() { return (int)ID; }
        public Group(Int16 id, string name, HashSet<UserGroup> UsersGroups, HashSet<GroupFieldAuthority> GroupsFieldsAuthority)
        { ID = id; Name = name; usersGroups = UsersGroups;  groupsFieldsAuthority = GroupsFieldsAuthority; }
    }
    public class GroupFieldAuthority : Object
    {
        public Group Group { get; private set; }
        public FieldDef FieldDef { get; private set; }
        public enumAuthRORWMADeny Authority { get; private set; }
        public override bool Equals(Object obj)
        {
            //Check for null and compare run-time types.
            if (obj == null || GetType() != obj.GetType()) return false;
            GroupFieldAuthority item = (GroupFieldAuthority)obj;
            return (Group.ID == item.Group.ID);
        }
        public override int GetHashCode() { return (int)Group.ID ^ (int)FieldDef.ID; }
        public GroupFieldAuthority(Group group, FieldDef fieldDef, enumAuthRORWMADeny authority)
        { Group = group; FieldDef = fieldDef; Authority = authority; }
    }
    public class FieldAuthority : Object
    {   // used for FieldDef and Document
        public FieldDef FieldDef { get; private set; }
        public enumAuthRORWMADeny Authority { get; private set; }
        public override bool Equals(Object obj)
        {
            //Check for null and compare run-time types.
            if (obj == null || GetType() != obj.GetType()) return false;
            FieldAuthority item = (FieldAuthority)obj;
            return (FieldDef.ID == item.FieldDef.ID);
        }
        public override int GetHashCode() { return (int)FieldDef.ID; }
        public FieldAuthority(FieldDef fieldDef, enumAuthRORWMADeny authority)  
        { FieldDef = fieldDef; Authority = authority; }
    }

【问题讨论】:

    标签: .net linq c#-4.0 linq-to-objects


    【解决方案1】:

    这就是你想要的吗?

    return groupsFieldsAuthority
             .Where(x => x.Group == this)
             .OrderBy(x => x.Group.Name)
             .Select(x => new FieldAuthority(x.FieldDef, x.Authority))
             .ToList();
    

    【讨论】:

    • 我想你的意思是 x => new FieldAuthority(x.FieldDef, x.Authority) ??无论如何编译。没有用数据测试它,但它看起来是正确的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 2019-06-08
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多