zoukankan      html  css  js  c++  java
  • 表中某列的所有值转成List泛型集合

    代码如下:

    //表dt中含有字段名称列
    List<string> fieldList = dt.AsEnumerable().Select(t => t.Field<string>("字段名称")).ToList<string>();
    //还可以
    List<string> fieldList2=(from d in dt.AsEnumerable() select d.Field<string>("字段名称")).ToList<string>();

    其中dt.AsEnumerable()得到datarow的集合,对于DataRow有一个Field<T>("列名")的方法:dr.Field<string>("字段名称"),得到字符串类型的值。

    扩展:Lambda表达式和Linq

        class Program
        {
            static void Main(string[] args)
            {
                LinqDataTable();
            }
            public static string[,] infoArr = new string[,] { { "1", "百度", "baidu", "201303" }, { "2", "迅雷", "xunlei", "201302" }, { "3", "谷歌", "guge", "201301" } };
            protected static void LinqDataTable()
            {
                DataRow row;
                ClientStruct cs = new ClientStruct();
                DataTable dtTable = new DataTable();
                dtTable.Columns.Add(cs.ID);
                dtTable.Columns.Add(cs.Name);
                dtTable.Columns.Add(cs.Company);
                dtTable.Columns.Add(cs.CreatedDate);
                for (int i = 0; i < 3; i++)
                {
                    row = dtTable.NewRow();
                    row[cs.ID] = infoArr[i, 0];
                    row[cs.Name] = infoArr[i, 1];
                    row[cs.Company] = infoArr[i, 2];
                    row[cs.CreatedDate] = infoArr[i, 3];
                    dtTable.Rows.Add(row);
                }
    
                //遍历DataTable,取出所有的ID
                //第一种:
                List<string> lstID = (from d in dtTable.AsEnumerable()
                                      select d.Field<string>(cs.ID)).ToList<string>();
    
    
                //第二种:
                List<string> allId = dtTable.AsEnumerable().Select(d => d.Field<string>("ID")).ToList();
    
                //遍历DataTable,将其中的数据对应到ClientStruct中
                //第一种:
                List<ClientStruct> list = (from x in dtTable.AsEnumerable()
                                           orderby x.Field<string>(cs.Company)
                                           select new ClientStruct
                                           {
                                               ID = x.Field<string>(cs.ID),
                                               Name = x.Field<string>(cs.Name),
                                               Company = x.Field<string>(cs.Company),
                                               CreatedDate = x.Field<string>(cs.CreatedDate)
                                           }).ToList<ClientStruct>();
    
                //第二种:
                List<ClientStruct> list2 = dtTable.AsEnumerable().OrderBy(o => o.Field<string>(cs.Company)).Select(x => new ClientStruct
                {
                    ID = x.Field<string>(cs.ID),
                    Name = x.Field<string>(cs.Name),
                    Company = x.Field<string>(cs.Company),
                    CreatedDate = x.Field<string>(cs.CreatedDate)
                }).ToList<ClientStruct>();
    
                //遍历DataTable,并将上面的List结果存储到Dictionary中:
                Dictionary<string, ClientStruct> dic = list.ToDictionary(p => p.Company);
    
                //p作为string键值来存储
            }
        }
        /*遍历DataTable*/
        class ClientStruct
        {
            public string ID = "ID";
            public string Name = "Name";
            public string Company = "Company";
            public string CreatedDate = "CreatedDate";
        }
    

      

  • 相关阅读:
    git 之gitignore 添加项之后生效的问题
    使用 padding-bottom 设置高度基于宽度的自适应
    ES5中新增的Array方法详细说明
    zepto.js 自定义打包集成其他模块构建流程
    移动端如何让页面强制横屏
    快来看看抓包工具有哪些?
    实践出真知,小程序wepy,uni-app框架开发使用!
    开发过程遇到的css样式问题记录
    带坑使用微信小程序框架WePY组件化开发项目,附带第三方插件使用坑
    微信 + weui 框架记录
  • 原文地址:https://www.cnblogs.com/Med1tator/p/6430403.html
Copyright © 2011-2022 走看看