所以我試圖用三種不同的方式來做一些事情,只是略微超出了我的理解邊緣。讓我解釋一下我在做什麼 然後我會詳細解釋我對 的理解。匿名的泛型代表表達了lambda表達式... Ithink
我有幾個詞典,其中包含我需要生成 報告的對象。他們都是
ConcurrentDictionary< Int64, List< EarningsReportCV>>
其中盈利CV是隻包含 特性(自定義視圖模型)的自定義對象。
我有三個這樣的字典......並且初始化他們的代碼是 幾乎相同,他們每個都只包含一個不同的類CV。
下面是一個例子:
private void BuildDictForAllEarn(List<EarningsReportCV> list, ConcurrentDictionary<Int64, List<EarningsReportCV>> theDict)
{
foreach (EarningsReportCV cv in list)
{
if (theDict.ContainsKey(Convert.ToInt64(cv.Ssn)))
{
//append in list already in Dict - EWB
theDict[ Convert.ToInt64(cv.Ssn) ].Add(cv);
}
else
{
//insert inital list into the Dict - EWB
List<EarningsReportCV> cvList = new List<EarningsReportCV>();
cvList.Add(cv);
theDict.AddOrUpdate(Convert.ToInt64(cv.Ssn), cvList, (foundkey, oldvalue => cvList);
}
}
}
所有三本字典是有方向性的斷絃<T>.Ssn
而不必只用類型的CV 變化的複製和粘貼代碼,我想使一個通用的方法。爲此,我需要在一個匿名委託中傳遞 ,這允許我一般採用類型T中傳遞的 並獲取它的.Ssn屬性作爲關鍵字。
我一派,思想和閱讀並走到這一步......
通用:
private void BuildDict<T>(List<T> list, ConcurrentDictionary<Int64, List<T>> theDict, Func<T, string> getIndexFunc)
{
foreach (T cv in list)
{
if (theDict.ContainsKey(Convert.ToInt64(getIndexFunc(cv))))
{
//append in list already in Dict - EWB
theDict[ Convert.ToInt64(getIndexFunc(cv)) ].Add(cv);
}
else
{
//insert inital list into the Dict - EWB
List<EarningsReportCV> cvList = new List<EarningsReportCV>();
cvList.Add(cv);
theDict.AddOrUpdate(Convert.ToInt64(getIndexFunc(cv)), cvList, (foundkey, oldvalue) => cvList);
}
}
}
我稱之爲正是如此
private void BuildDictForAllEarnLAMBDA(List<EarningsReportCV> list, ConcurrentDictionary<Int64, List<EarningsReportCV>> theDict)
{
BuildDict<EarningsReportCV>(list, theDict, (T) => { return T.Ssn; });// fix this lambda as paramether stuff...- EWB
}
我認爲我得到的一切,除了有3r d參數,其中I 想要作爲lambda傳遞以查找的.Ssn屬性通用類型<T>
。
當我編譯它,我得到這些錯誤..
錯誤43參數1:無法從 'T' 轉換成 'EFRGPayroll3G.CV.EarningsReportCV' C:\用戶\ Brown.Ericw \文檔\ Visual Studio中 \項目\ WindowsService1 \ WindowsService1 \ BLL \ RazorReportRenderBLL.cs 406 33 WindowsService1
錯誤45參數2:不能從'System.Collections.Generic.List<EFRGPayroll3G.CV.EarningsReportCV>'
到'System.Func<long,System.Collections.Generic.List<T>>'
ç轉換:\用戶\ Brown.Ericw \文檔\ Visual Studi ø 2013 \項目\ WindowsService1 \ WindowsService1 \ BLL \ RazorReportRenderBLL.cs 407 81 WindowsService1
錯誤46參數3:無法從 'lambda表達式' 轉換到'System.Func<long,System.Collections.Generic.List<T>,System.Collections.Generic.List<T>>'
C:\用戶\ Brown.Ericw \文檔\ Visual Studio中 2013 \項目\ WindowsService1 \ WindowsService1 \ BLL \ RazorReportRenderBLL。CS 407 89 WindowsService1
誤差44,用於'System.Collections.Concurrent.ConcurrentDictionary<long,System.Collections.Generic.List<T>>.AddOrUpdate(long, System.Func<long,System.Collections.Generic.List<T>> System.Func<long,System.Collections.Generic.List<T>, System.Collections.Generic.List<T>>)'
最好重載的方法匹配具有一些無效參數C:\用戶\ Brown.Ericw \文件\視覺 工作室 2013 \項目\ WindowsService1 \ WindowsService1 \ BLL \ RazorReportRenderBLL.cs 407 21 WindowsService1
誤差42,用於'System.Collections.Generic.List<EFRGPayroll3G.CV.EarningsReportCV>.Add(EFRGPayroll3G.CV.EarningsReportCV)'
最好重載的方法匹配具有一些無效參數C:\用戶\ Brown.Ericw \文檔\ Visual Studio中 \項目\ WindowsService1 \ WindowsService1 \ BLL \ RazorReportRenderBLL.cs 406 21 WindowsService1
在這一點上,我不再理解正在發生什麼事......我的大腦已經滿了......我該怎麼做才能將它用於功能性代碼片段。我正在尋找做什麼,以及我需要什麼來包裝我的頭,以及所有優秀的文章,以向我解釋它......任何甚至小的理解閃光都非常感謝。
你爲什麼寫了近你的整個問題是引用的文字? – 2014-09-12 14:28:49
我不知道,當我試圖引用錯誤時,編輯會這樣做,我回去手動修復它,因爲「unquote」按鈕不起作用......它只是將它留在那裏..我只是想在回去之前保存它。看起來你打敗了我。謝謝喬恩! – 2014-09-12 14:30:00