运用Linq,将List<T> 转换为 Dictionary<T Key,T Value>

即:List<T>  ToDictionary<T Key,T Value>

废话不说,看代码:

示例代码:按照性别分类

class Program
{
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public int Sex { get; set; }
} static void Main(string[] args)
{
List<Student> lstStudent = new List<Student>();
lstStudent.Add(new Student { Name = "A1", Age = , Sex = });
lstStudent.Add(new Student { Name = "A2", Age = , Sex = });
lstStudent.Add(new Student { Name = "A3", Age = , Sex = });
lstStudent.Add(new Student { Name = "A4", Age = , Sex = });
lstStudent.Add(new Student { Name = "A5", Age = , Sex = });
lstStudent.Add(new Student { Name = "A6", Age = , Sex = });
lstStudent.Add(new Student { Name = "A7", Age = , Sex = });
lstStudent.Add(new Student { Name = "A8", Age = , Sex = });
lstStudent.Add(new Student { Name = "A9", Age = , Sex = }); var dicStudent = lstStudent.GroupBy(p => p.Sex).ToDictionary(p => p.Key, p => p.ToList());
foreach (var dic in dicStudent)
{
Console.WriteLine("Key:{0}", dic.Key);
foreach (var stu in dic.Value)
{
Console.WriteLine("Name:{0},Age:{1}", stu.Name, stu.Sex.ToString());
}
}
Console.ReadKey();
}
}

实际运行结果:

Key:
Name:A1,Age:
Name:A4,Age:
Name:A6,Age:
Name:A8,Age:
Name:A9,Age:
Key:
Name:A2,Age:
Name:A3,Age:
Name:A5,Age:
Name:A7,Age:

轻松利用linQ

linq学习笔记:将List<T> 转换为 Dictionary<T Key,T Value>的更多相关文章

  1. C# LINQ学习笔记一:走进LINQ的世界

    本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5832322.html,记录一下学习过程以备后续查用. LINQ 简介:     语言集成查询(LINQ)是Vi ...

  2. linq学习笔记

    最近在学习linq的一些基础知识,看了c#高级编程及阅读了园子内部几篇优秀的博文,有所体会,感觉应该记录下来,作为以后复习使用.都是一些最基础的知识,大致分为三个部分:linq预备知识:linq查询: ...

  3. (转)Linq学习笔记

    写在前面 最近在看Linq,在博客园看到这篇文章,写的通俗易懂,转来和大家一起做个分享.原文地址http://www.cnblogs.com/goscan/archive/2011/05/05/Lin ...

  4. C#之Linq学习笔记【转】

    写在前面 其实在09年就已经学习过Linq了,并被她那优美的语法所吸引,只是现在所在的公司还在使用VS2005在.Net2.0的框架下面的开发,所以Linq也很久没有用过了,最近看部门的同事对这个有些 ...

  5. C# LINQ学习笔记二:LINQ标准查询操作概述

    本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5801249.html,记录一下学习过程以备后续查用. “标准查询运算符”是组成语言集成查询 (LINQ) 模式 ...

  6. C# Linq 学习笔记

    刚刚学习了 Siki老师 的C#教程Linq部分,以下是笔记 需要引用命名空间 using System.Linq; 然后我们需要准备数据 武林高手类 /// <summary> /// ...

  7. C# LINQ学习笔记三:LINQ to OBJECT之操作字符串

    本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5814204.html,记录一下学习过程以备后续查用. 一.统计单词在字符串中出现的次数 请注意,若要执行计数, ...

  8. LINQ 学习笔记(1)

    学习资源参考 : http://www.cnblogs.com/lifepoem/archive/2011/12/16/2288017.html 常用方法是 Where, OrderBy, Selec ...

  9. C# LINQ学习笔记

    LINQ,语言集成查询: LINQ TO SQL,同EF,NHibernate一样,也是一种ORM框架: 1. 入门应用示例: static public void LinqBasic() { var ...

随机推荐

  1. cheerio返回数据格式

    通读cheerio API { options: { decodeEntities: false, withDomLvl1: true, normalizeWhitespace: false, xml ...

  2. bzoj 3611 [Heoi2014]大工程(虚树+DP)

    3611: [Heoi2014]大工程 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 408  Solved: 190[Submit][Status] ...

  3. CSS3 概览 更新时间 2014-0412-1317

    CSS3 概览 CSS3可以划分为:文字.边框模型.背景.动画等. CSS3颜色模块 CSS2.1的时候可以使用4种颜色方式,直接使用颜色名,如 redRGB值,如 rgb(0,90,255)RGB百 ...

  4. Google生活

    本博文的主要内容有 .Google生活的介绍 1.Google生活的介绍 Google生活搜索的官网: http://shenghuo.google.cn/shenghuo/ http://sheng ...

  5. 第十七章、程序管理与 SELinux 初探

    ---恢复内容开始--- 什么是程序 (process) 在 Linux 底下所有的命令与你能够进行的动作都与权限有关, 而系统依据UID/GID以及文件的属性相关性判定你的权限!在 Linux 系统 ...

  6. hdu 1015 dfs

    Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...

  7. poj3624背包问题(一维数组)

    Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd ...

  8. MySQL 基础(DDL)

    SQL分类         SQL语句主要可以划分为一下3个类别      DDL:数据定义语言,定义数据段.数据库.数据表等      DML :数据操纵语句,用于添加.删除.更新和查询数据库记录 ...

  9. MapObject shape数据操作

    利用MO 的GeoDataset  .DataConnection  Recordset 进行数据操作 在MO中.使用GeoDataset对象和DataConnection 对象来连接和读取地理数据. ...

  10. 单元测试时候使用[ClassInitialize]会该方法必须是静态的公共方法,不返回值并且应采用一个TestContext类型的参数报错的解决办法

    using Microsoft.VisualStudio.TestTools.UnitTesting; 如果该DLL应用的是 C:\Program Files\Microsoft Visual Stu ...