我想Yield在 return 陳述句中使用關鍵字,但是在使用它之后我遇到了強制轉換問題:
無法將型別“IEnumerable - Mderator”隱式轉換為“Moderator”。存在顯式轉換(您是否缺少演員表?)
當我洗掉Yield關鍵字時,我沒有錯誤,并且我不想使用dynamicas 型別回傳。
這是我的方法:
public IEnumerable<Moderator> GetAllModerators(int id)
{
RtcRepository repoRtc = new RtcRepository(db);
yield return repoRtc.GetByID(id).Collection1.SelectMany(x => x.Collection2.Select(y => y.Moderator));
}
uj5u.com熱心網友回復:
您誤解了收益率的使用。yield return 用于從基本型別的多個連續回傳生成 IEnumerable。
例如:
for (int i = 0; i < 100; i )
yield return i;
將生成一個 IEnumerable。
在這里,您已經有了一個列舉,因此您根本不需要 yield。
uj5u.com熱心網友回復:
在這種情況下,您不需要屈服。試試這個
public IEnumerable<Moderator> GetAllModerators(int id)
{
RtcRepository repoRtc = new RtcRepository(db);
return repoRtc.GetByID(id).Collection1
.SelectMany(x => x.Collection2.Select(y => y.Moderator)).FirstOrDefault();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/394756.html
