Linq 操作DataTable
来源:互联网 发布:hycad软件使用视频 编辑:程序博客网 时间:2024/04/30 22:01
class ClientStruct { public string ID = "ID"; public string Name = "Name"; public string Company = "Company"; public string CreatedDate = "CreatedDate"; }
public string[,] infoArr = new string[,] { { "1", "百度", "baidu", "201303" }, { "2", "迅雷", "xunlei", "201302" }, { "3", "谷歌", "guge", "201301" } };
protected 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>(); //遍历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>(); //遍历DataTable,并将上面的List结果存储到Dictionary中: Dictionary<string, ClientStruct> dic = list.ToDictionary(p => p.Company); //p作为string键值来存储 }
其实关键是AsEnumerable()方法,返回一个 System.Collections.Generic.IEnumerable<T> 对象
阅读全文
0 0
- Linq to datatable 操作
- Linq操作DataTable
- c# Linq操作DataTable
- Linq 操作DataTable
- Linq 操作DataTable
- Linq to DataTable 操作
- Linq 操作DataTable
- Linq 操作DataTable
- C# 中 Linq 操作 DataTable
- LINQ to DataSet的DataTable操作
- LINQ系列:LINQ to DataSet的DataTable操作
- LINQ系列:LINQ to DataSet的DataTable操作
- Linq Datatable
- datatable linq
- Linq To DataTable
- LINQ To DataTable
- Datatable中linq查询
- Linq To DataTable
- Oracle数据库 row_number() over() 函数使用说明
- jvm-垃圾收集器
- 目录——Javase第二阶段:OOP
- 缺省配置Springboot Web应用中tomcat的启动过程
- 详解Java8特性之新的日期时间 API
- Linq 操作DataTable
- OC 产生某个范围的随机数
- PHP学习笔记(一)——PHP的生命周期、运行机制及并发模型
- JS中5种原始数据类型
- HBase HMaster状态异常
- 11.25整理
- java 设计模式 观察者模式
- linux coredump介绍
- spring AOP 领悟