我想從LINQ查詢返回的一組行中找到最早的一年。 ActivationDate作爲日期時間值存儲在數據庫中。從LINQ查詢結果中獲取最低年份
我知道我可以做一個單獨的查詢來獲取日期,但我寧願使用現有查詢的結果,因爲它用於其他幾件事情。
IEnumerable<MonitoringPanel> panels = from rows in db.MonitoringPanels
where rows.DealerEntity == dealerIDint
select rows;
但是這個不斷拋出一個錯誤:
var testDate = panels.Min().ActivationDate;
錯誤:
System.ArgumentException was unhandled by user code.
即使我嘗試選擇最低PanelNumner(存儲爲一個int),它會拋出一個錯誤,但以下工作確實有效:
var testDate = panels.FirstOrDefault().ActivationDate;
解決方案
DateTime testDate = (DateTime)panels.Min(thing => thing.ActivationDate);
int lowYear = testDate.Year;
Welcom,Mason。你似乎忘了提到實際的錯誤信息? – Adam
請提供錯誤消息。但最初,你在事物上使用'Min'而不是在第一個實例中查詢事物的日期(而應該是'panels.Min(thing => thing.ActivationDate).ActivationDate;'。 –