开发者

DataTable.Rows method doesn't return an IEnumerable<DataRow>

I am a big fan of using 'var' in C# rather than typing out all the data types. I find it annoying that

var dt = CreateAndPopulateDataTable()   // Immaterial
foreach (var row in dt.Rows)

returns th开发者_如何学编程e 'row' as an object variable rather than a DataRow variable. I know I can cast it later or just use 'DataRow' rather than 'var'. Are those my only two options?


You can get an IEnumerable<DataRow> by using the AsEnumerable() method against the table.

foreach (var row in dt.AsEnumerable())

However, in this case, you are hardly saving characters verus simply going ahead and specifying the type.

foreach (DataRow row in dt.Rows) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜