Convert DataTable to Generic List in C#

后端 未结 9 1303
既然无缘
既然无缘 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:14

    Here is a simple way to convert to generic list in c# with Where condition

    List filter = ds.Tables[0].AsEnumerable()
                            .Where(x => x.Field("FilterID") == 5)
                            .Select(row => new Filter
                            {
                                FilterID = row.Field("FilterID"),
                                FilterName = row.Field("FilterName")
                            }).ToList();
    

    First Define properties and use as per

    public class Filter
    {
        public int FilterID { get; set; }
        public string FilterName { get; set; }
    }
    

    Put Package:

    using System.Linq;
    using System.Collections.Generic;
    

提交回复
热议问题