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()); //....


原创粉丝点击