2

使用类 RoleRecord (Guid RoleId, string RoleName,...) 我试图获取一个新的 Name 列表,其中 RoleId 与 Guid 列表匹配

IEnumerable<RoleRecord> roles;
IEnumerable<Guid> roleIds;

我正在考虑避免嵌套的 for 循环,并且沿着:

            var query = 
            from rowA in roles
            join rowB in roleIds
            on rowA.RoleId equals rowB.????
            select { rowA.RoleName };

我也尝试将 guid 包装在一个类中,但由于语法错误,甚至无法构建它。有任何想法吗?谢谢

4

2 回答 2

5

如果您有大量 Guid,我个人不会使用 Jeremy 的答案。如果 join 是您真正想要表达的 - 您只需要:

var query = from rowA in roles
            join rowB in roleIds on rowA.RoleId equals rowB
            select rowA.RoleName;

或者,首先创建一组角色 ID:

HashSet<Guid> validRoleIds = new HashSet<Guid>(roleIds);
var query = from rowA in roles
            where validRoleIds.Contains(rowA.RoleId)
            select rowA.RoleName;

The advantage is that then you don't need to do a linear search through every valid role ID for every role. That's not an issue if you know you don't have many roles or role IDs, but generally a hashing approach will be more effective.

Note that the join will use a hash as well internally.

于 2009-05-22T05:42:15.900 回答
3

试试这个:

var query = 
            from rowA in roles
            where roleIds.Contains(rowA.RoleId)
            select  rowA.RoleName;
于 2009-05-22T00:04:06.373 回答