2012-03-29 65 views
1

我得到了下面的代碼:將對象添加到LINQ中的字典值?

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; 
} 

我想改變Ditionary在Dictionary<int?, ScanClass>

ScanClass是一個對象

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

是否可以給出一個對象字典的價值? PS我不想用查找來做這件事。

在此先感謝!

回答

4

聽起來像是你可能想是這樣的:

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(),除非您有原因將其並行化。

+0

Jon quick question hair&scanFileAvatar是2MB的大數據通道,如果我執行這個查詢,我會得到一個內存不足的異常我避免或解決這個問題? – dg90 2012-03-29 13:30:00

+0

@daageu:大概不要試圖一次性獲取數據庫的全部內容...還沒有足夠的信息來說明你想要做出什麼來給出更好的答案...... – 2012-03-29 13:48:43

1
_scanDictionaryLocal = ctxLocal.tblScan 
    .ToDictionary(s => s.MEMBERID, s => new ScanClass() 
    { 
     ScanFileAvatar = s.scanFileAvatar, 
     Hair = s.hair 
    }); 
+0

我只是跳過'Select'和'AsParallel'並直接執行'ToDictionary' – Magnus 2012-03-29 13:07:02