有了LINQ,如何做select並只返回第一個關聯的子項?LINQ,只選擇子項的第一個元素
我:
[table: Report]
GeneratedDate
PerformanceMetric 1 -----> N [table PerformanceMetric]
Bios Timestamp
... X
Y
..... (there is a good dozen other fields)
目前,我做的:
Report = _ctx.Reports.SingleorDefault(rpt => rpt.bios == mySerial);
這將返回與所有PerformanceMetric相關的報告。我想只返回PerformanceMetric的第一個匹配項。
LINQ是否提供了這樣做的內置方法?
編輯:其實,我正在尋找報告,但PerformanceMetric應該只包含第一個元素。 (Report.PerformanceMetrics.Count < = 1) sry不清楚..
所以你確實有正確的實現一直?或者你想要報告,但「PerformanceMetric」應該只包含第一個元素? –
是包含Report.PerformanceMetrics的報告僅包含1或0個元素。可能會有1000個與數據庫中的報告相關聯的PerformanceMetric記錄... – Apoulet
好吧,讓我直接得到它,您想要報告,但您希望修改報告以便PerformanceMetric僅包含0/1元素? –