Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C Linq查询返回字典_C#_Arrays_Linq_Dictionary_Int - Fatal编程技术网

C# C Linq查询返回字典

C# C Linq查询返回字典,c#,arrays,linq,dictionary,int,C#,Arrays,Linq,Dictionary,Int,好的,我需要根据Linq查询的结果创建/返回一个字典。我已经尝试了我能想到的一切,并不断遇到问题。以下是我目前正在尝试的 public static Dictionary<int,int[]> GetEntityAuthorizations(int userId) { using (MyDb db = new MyDb()) { var query = db.EntityManagerRoleAssignments.Wh

好的,我需要根据Linq查询的结果创建/返回一个字典。我已经尝试了我能想到的一切,并不断遇到问题。以下是我目前正在尝试的

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;
        }
    }
任何帮助都将不胜感激。我希望从这个字典中返回的是一个键为entityId或EntityManager.entityId的字典,值是一个关联RoleId的数组

目前,我在编译时遇到以下两个错误,其他尝试也出现了类似的错误,但与这些错误并不完全相同

错误11无法从用法推断方法“System.Linq.Enumerable.ToDictionarySystem.Collections.Generic.IEnumerable、System.Func、System.Collections.Generic.IEqualityComparer”的类型参数。尝试显式指定类型参数

错误12无法将类型“System.Collections.Generic.Dictionary”隐式转换为“System.Collections.Generic.Dictionary”

更新-感谢@D Stanley的有效解决方案


听起来您希望按实体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;
}

它目前在做什么,而不是你想要它做什么?为什么你想要一本只有一对的词典?该方法采用单个用户标识,因此此词典不会包含多个keyvaluepair。让它返回一个int[]。您的权利,我已经编辑了我的代码,以更好地显示我的目标…键将是entityId,抱歉。您只是想将两个枚举值组合在一起。您可能正在query.ToDictionaryx=>x.EntityManager.EntityId,x=>x.RoleId的行中查找某些内容。但这里RoleId不是数组。试试这个。谢谢D Stanley。我认为这是完美的D Stanley,在第一次运行时,它似乎有3个条目,第一个是正确的,第二个是0,0。但是,在代码块完成后,GetEntityAuthorizations只有一个集合,这是正确的。很高兴它能工作。通过检查Linq查询来调试它们可能很棘手。通常你需要查看最终结果,而不是检查中间结果。是的,没错,我很难得到任何半正确的结果来查看最终结果。非常感谢你!
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;
}