c# - 将对象添加到 LINQ 中的字典值?

标签 c# linq entity-framework dictionary

我得到了下一个代码:

private Dictionary<int?, byte[]> GetAllLocalScanFiles()
{
    using (DZine_IStylingEntities ctxLocal = new DZine_IStylingEntities())
    {
        _scanDictionaryLocal = ctxLocal.tblScan
                .Select(s => new { s.MEMBERID, s.scanFileAvatar })
                .AsParallel()
                .ToDictionary(s => s.MEMBERID, s => s.scanFileAvatar);
    }
    return _scanDictionaryLocal;
}

我想更改 Dictionary<int?, ScanClass> 中的词典

ScanClass 是一个对象

public class ScanClass
{
    public byte[] ScanFileAvatar { get; set; }
    public byte[] Hair { get; set; }
}

是否可以为字典的值提供一个对象? PS 我不想通过查找来执行此操作。

提前致谢!

最佳答案

听起来你可能想要这样的东西:

private Dictionary<int?, ScanClass> GetAllLocalScanFiles()
{
    using (DZine_IStylingEntities ctxLocal = new DZine_IStylingEntities())
    {
        return ctxLocal.tblScan
                 .Select(s => new { s.MEMBERID, s.scanFileAvatar, s.hair })
                 .AsParallel()
                 .ToDictionary(s => s.MEMBERID, s => new ScanClass {
                                   hair = s.hair,
                                   scanFileAvatar = s.scanFileAvatar
                               });
    }
}

假设您的原始实体中有一个 hair 属性。

目前尚不清楚 AsParallel 是否真的能帮助您。您可能需要考虑改用 AsEnumerable(),除非您有理由对此进行并行化。

关于c# - 将对象添加到 LINQ 中的字典值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925799/

相关文章:

c# - 如何按键查找/检查字典值

C# 代码在 Pinvoke SendMessage 之后挂起

c# - 强行删除一个文件,即使它正在被其他进程使用

c# - .NET、.Contains() 或 .Count() 哪个更快?

c# - 扩展方法 & LINQ to Entities 无法识别方法错误

entity-framework - EF Core 中的表值函数

c# - VS Entity Framework 中的Oracle实体不更新代码中的主键

c# - EventHubProducerClient 是否提供带有单个 EventData 的 SendAsync?

linq - 向匿名 Linq 结果集添加人工行

c# - LINQ:查找最大元素(不是最大值)