0
內使用,我用下面的代碼其中工程:Yield語句不能匿名方法
IEnumerable<Tag> CreateTags()
{
var suppliers = SupplierQuery.MatchTerms(Min, Max);
foreach (var item in suppliers)
{
var text = item.Name;
var route = PageRoute.GetSupplierRoute(item.Name);
yield return new Tag(text, route);
}
}
我一直在試圖鏈這2個statments用在一起IEnumerable.ForEach擴展方法,如下所示:
IEnumerable<Tag> CreateTags()
{
var suppliers = SupplierQuery.MatchTerms(Min, Max)
.ForEach(x =>
{
yield return new Tag(x.Name, PageRoute.GetSupplierRoute(x.Name));
});
}
但是,我得到一個錯誤 - 「Yield語句不能匿名方法的內部使用」 - 有什麼辦法解決這無需創建一個新的List<Tag>
還是必須分開?
在此先感謝。
沒有'IEnumerable.ForEach'擴展方法。你有一個你自己寫的自定義擴展方法,或者你的意思是'List .ForEach' –
2015-02-24 14:55:42