C#: yield return within a foreach fails – body cannot be an iterator block

You can only use yield return in a function that returns an IEnumerable or an IEnumerator, not a List<T>.

You need to change your function to return an IEnumerable<DesktopComputer>.

Alternatively, you can rewrite the function to use List<T>.ConvertAll:

return GetComputerIdTags().ConvertAll(pcTag => 
    new DesktopComputer() {
        AssetTag    = pcTag,
        Description = "PC " + pcTag,
        AcquireDate = DateTime.Now
    });

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)