Linq To DataTable结合Dictionary,List实例讲解

111 篇文章 0 订阅
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()); //....


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值