简体   繁体   English

C#Linq查询以返回Dictionary <int,int[]>

[英]C# Linq query to return Dictionary<int,int[]>

Ok, so I have a need to create/return a Dictionary from the results of a Linq Query. 好的,因此我需要根据Linq查询的结果创建/返回字典。 I have tried just about everything I can think of and keep running into issues. 我已经尝试了几乎所有我能想到的东西,并不断遇到问题。 Here is what I am currently attempting... 这是我目前正在尝试的...

public static Dictionary<int,int[]> GetEntityAuthorizations(int userId)
    {
        using (MyDb db = new MyDb())
        {
            var query = db.EntityManagerRoleAssignments.Where(x => x.EntityManager.ManagerId == userId);
            var entityId = query.Select(x => x.EntityManager.EntityId);
            var roles = query.Select(x => x.RoleId).ToArray();
            var result = query.ToDictionary(entityId, roles);
            return result;
        }
    }

any help at all would be greatly appreciated. 任何帮助将不胜感激。 what i am looking for to be returned from this is a Dictionary where the Key is the entityId or EntityManager.EntityId and the Value(s) are an array of associated RoleId's. 我正在寻找从中返回的是一个字典,其中的键是entityId或EntityManager.EntityId,值是关联的RoleId的数组。

Currently I am getting the following two errors at compile time, and other attempts have been errors similar but not exact to these. 目前,我在编译时遇到以下两个错误,而其他尝试则是类似但不准确的错误。

Error 11 The type arguments for method 'System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable, System.Func<TSource,TKey>, System.Collections.Generic.IEqualityComparer<TKey>)' cannot be inferred from the usage. 错误11方法'System.Linq.Enumerable.ToDictionary <TSource,TKey>(System.Collections.Generic.IEnumerable,System.Func <TSource,TKey>,System.Collections.Generic.IEqualityComparer <TKey>)的类型参数无法从用法中推断出来。 Try specifying the type arguments explicitly. 尝试显式指定类型参数。

Error 12 Cannot implicitly convert type 'System.Collections.Generic.Dictionary<TKey,Sqor.Database.DbEntityManagerRoleAssignment>' to 'System.Collections.Generic.Dictionary<int,int[]>' 错误12无法将类型'System.Collections.Generic.Dictionary <TKey,Sqor.Database.DbEntityManagerRoleAssignment>'隐式转换为'System.Collections.Generic.Dictionary <int,int []>'

UPDATE - Working Solution (Thanks to @D Stanley) 更新-工作解决方案(感谢@D Stanley)

public static Dictionary<int,int[]> GetEntityAuthorizations(int userId)
    {
        using (SqorDb db = new SqorDb())
        {
            var query = db.EntityManagerRoleAssignments.Where(x => x.EntityManager.ManagerId == userId);
            var entityGroups = query.GroupBy(x => x.EntityManager.EntityId);
            var result = entityGroups.ToDictionary(e => e.Key,
                                                   g => g.Select(x => x.RoleId).ToArray()
                                                  );
            return result;
        }
    }

It sounds like you want to group by the Entity ID and project the associated role IDs to an array: 听起来您想按实体ID分组并将关联的角色ID投影到数组中:

using (MyDb db = new MyDb())
{
    var query = db.EntityManagerRoleAssignments.Where(x => x.EntityManager.ManagerId == userId);
    var entityGroups = query.GroupBy(x => x.EntityManager.EntityId);
    var result = entityGroups.ToDictionary(e => e.Key, 
                                           g => g.Select(x => x.RoleId).ToArray()
                                          );
    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM