Convert DataTable to Generic List in C#

后端 未结 9 1299
既然无缘
既然无缘 2020-12-01 01:28

Disclaimer: I know its asked at so many places at SO.
My query is a little different.

Coding Language: C# 3.5

I have a DataTable named cardsTable that

9条回答
  •  悲哀的现实
    2020-12-01 02:27

    well its the one line solution

    it depends on whether or not you know the data in the database is all valid and will not contain anything that will break the above

    eg a nullable field whenre you dont expect it - maybe due to a left join int eh sql that genertates the data.

    So if you have validated the data before then yeah - I was goign to suggest some linq - but you got tht down.

    If you need some validation however you should probably just loop through the datarows, generate your object as above and add it to the collection ... this will also allow you to handle errors in one row and still process the rest.

    Thats the way i see it anyway

    (damn i came on to downvote something so my rep was 1024)

提交回复
热议问题