我有一個愚蠢但braincracker問題關於鑄造類型。從代碼中可以看到,我有一個變量lprod_monthyreport,取決於ytm值可以是List<Monthly_Report>
或簡單地Monthly_Report
。我需要變量在兩種情況下都具有相同的名稱。轉換對象T到列表中<T> C#
var lprod_monthlyReport = new List<Monthly_Report>;
if (ytm == true)
{
lprod_monthlyReport = _ProductRep.GetSpecificArchiveReport(prod.Prod_ID, lmonth, lyear, item.item_ID);
}
else
lprod_monthlyReport = _ProductRep.GetSpecificYTMReport(prod.Prod_ID, item.item_ID);
的問題是,如果我聲明,如果每一個(或其他)段內的變量的編譯器,因爲它說,該變量在這方面已經宣佈給錯誤。
我已經嘗試過鑄造
lprod_monthlyReport = (Monthly_Report) _ProductRep.GetSpecificArchiveReport(prod.Prod_ID, lmonth, lyear, item.Item_ID);
但它不工作。我也嘗試作爲關鍵字沒有成功。
請幫我用這個嗎?謝謝
弗朗切斯科
GetSpecificArchiveReport和GetSpecificYTMReport是什麼類型返回 – Hath 2011-04-14 12:58:36
爲什麼'lprod_monthlyReport' **有**要成爲'List',而不是你的方法返回什麼? –
2011-04-14 13:01:07
@Hath:謝謝你的回答。 ** GetSpecificArchiveReport **返回** Monthly_Report **對象(它使用** Single **進行LINQ查詢)** GetSpecificYTMReport **返回**列表 **(使用**的LINQ查詢** ** )。 –
CiccioMiami
2011-04-14 13:03:11