Linq To DataTable结合Dictionary,List实例讲解
来源:互联网 发布:仓管王软件 编辑:程序博客网 时间:2024/06/07 14:24
1.先来看个例子:
/* * Dictionary是表示键和值的集合,Dictionary<(TKey, TValue>)>泛型类提供了从一组键到一组值的映射。 * 字典中的每个添加项都由一个值及其相关联的键组成。通过键来检索值的速度是非常快的,接近于 O(1), * 这是因为 Dictionary<(TKey, TValue>) 类是作为一个哈希表来实现的。但是里面的值必须是唯一的才行。其中Tkey是字典中的键的类型,TValue是字典中的值的类型。 * 对于已经存在的Dictionary对象,我们可以通过这样的方式来获取里面的值 dic["key"] = "value"; * 如果想对其遍历的话,可以使用这样的方式foreach (KeyValuePair<string, string> kvp in openWith) //遍历输出里面的值 * Response.Write(kvp .Key +" "+kvp .Value +"<br>"); * 对于枚举而言,字典中的每一项都被视为一个表示值及其键的 KeyValuePair<(TKey, TValue)> 结构进行处理。 */
class Student { public string Name { get; set; } public int[] Scores { get; set; } }
protected void DictionaryTest() { Dictionary<int, Student> dicStudent = new Dictionary<int, Student>(); dicStudent.Add(1, new Student { Name = "Svetlana", Scores = new int[] { 98, 92, 81, 60 } }); dicStudent.Add(2, new Student { Name = "Claire", Scores = new int[] { 75, 84, 91, 39 } }); dicStudent.Add(3, new Student { Name = "Sven", Scores = new int[] { 88, 94, 65, 91 } }); dicStudent.Add(4, new Student { Name = "Cesar", Scores = new int[] { 97, 89, 85, 82 } }); var values = from x in dicStudent let temp = x.Value.Scores.Sum() orderby temp select new { name = x.Value.Name, totalsocre = temp }; values.ToList().ForEach(rs => Response.Write(string.Format("学生姓名:{0},总分是:{1}<br />", rs.name, rs.totalsocre))); }输出如下:
2.一些实际操作
Dictionary<int, string> dictionary; //.... string cacheName = CacheConfig.GetCacheName(CacheName.HR_Step__State_Tag); if (CacheHelper.DoNetCache.CacheExists(cacheName)) dictionary = CacheHelper.DoNetCache.GetCache(cacheName) as Dictionary<int, string>; else { dictionary = Globals.GetStatusByTypeName(PublicEnum.SystemStatusType.HR_Step_State_Tag); CacheHelper.DoNetCache.AddCache(cacheName, (object)dictionary); } var items = from i in dictionary where i.Value.Trim().Split('_')[1].StartsWith(_pValue) select new { key = i.Key, value = i.Value }; //linq过滤 DataTable dt = new DataTable(); dt.Columns.Add("ITLES_VALUE", typeof(int)); dt.Columns.Add("ITLES_TEXT", typeof(string)); items.ToList().ForEach(kv => dt.Rows.Add(kv.key, kv.value.Split('_')[0])); BindDropDownList(ddlStepStateTag, dt, "ITLES_TEXT", "ITLES_VALUE", new ListItem()); //....
- Linq To DataTable结合Dictionary,List实例讲解
- Linq To DataTable
- Linq to datatable 操作
- LINQ To DataTable
- Linq To DataTable
- linq to datatable
- Linq to DataTable 操作
- LINQ to DataSet/DataTable
- linq to datatable sum
- DataTable通过Linq转List
- linq实例讲解
- Linq to DataTable之Join
- Funny Linq Part3: Linq To DataTable
- List to dataTable
- DataTable + List to Json
- datatable to list
- 泛型--datatable TO List
- LINQ to SQL实例
- dom4j生成FusionChart所需xml字符串
- 用C#压缩和修复Access数据库
- FFMPEG windows build procedure
- 传输层的各种模式——ZeroMQ 库的使用
- android 启动页 实现
- Linq To DataTable结合Dictionary,List实例讲解
- yum命令的用法
- 数字版权保护技术
- 调用数典 ,实现下拉框
- 字符串问题
- 改小matlab的绘图大小、字号、线宽
- Java解析 properties
- FBO中如何使用模板缓冲区
- java小算法(2)