Why does foreach fail to find my GetEnumerator extension method?

前端 未结 8 1976
暗喜
暗喜 2021-02-18 21:19

I\'m trying to make some code more readable. For Example foreach(var row in table) {...} rather than foreach(DataRow row in table.Rows) {...}.

8条回答
  •  攒了一身酷
    2021-02-18 21:51

    some offtopic: if you want to do it more readable write

    foreach ( DataRow r in tbl.Rows ) yield return r;
    

    as

    foreach (DataRow row in tbl.Rows) 
    {
        yield return row;
    }
    

    now to your problem.. try this

        public static IEnumerable GetEnumerator(this DataTable table)
        {
            return table.Rows.Cast();
        }
    

提交回复
热议问题