本篇文章会向大家实例讲述以下内容:

  • 将数组转换为List
  • 将List转换为数组
  • 将数组转换为Dictionary
  • 将Dictionary 转换为数组
  • 将List转换为Dictionary
  • 将Dictionary转换为List
  • IQueryable,IEnumerable,List相互转换

首先这里定义了一个“Student”的类,它有三个自动实现属性。

class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}

将数组转换为List

将数组转换成一个List,我先创建了一个student类型的数组。

static void Main (string[] args)
{
//创建数组
Student[] StudentArray = new Student[];
//创建创建3个student对象,并赋值给数组的每一个元素 StudentArray[0] = new Student()
{
Id = ,
Name ="Tony Stark",
Gender ="Male"
};
StudentArray[] = new Student()
{
Id = ,
Name="Hulk",
Gender = "Male"
};
StudentArray[] = new Student()
{
Id = ,
Name ="Black Widow",
Gender="Female"
};

接下来,使用foreach遍历这个数组。

foreach (Student student in StudentArray)
{
Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender);
}

运行程序

接下来将这个数组转换为List,我们添加System.Linq命名空间,然后调用ToList()扩展方法。这里我们就调用StudentArray.ToList()

注意这个ToList方法的返回类型,它返回的是List< Student >对象,这说明我们可以创建一个该类型的对象来保存ToList方法返回的数据。

List<Student> StudentList = StudentArray.ToList<Student>();

使用foreach从StudentList中获取所有的学生资料。

List<Student> StudentList = StudentArray.ToList<Student>();

foreach (Student student in StudentList)
{
Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender);
}

运行程序

List转换为数组

将List转换为数组,使用System.Linq命名空间下的ToArray()扩展方法。

Student[] ListToArray = StudentList.ToArray<Student>();

使用foreach遍历学生资料

foreach (Student student in ListToArray)
{
Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender);
}

运行程序

 

将数组转换为Dictionary
将数组转换成Dictionary,使用ToDictionary()扩展方法。这里就可以用StudentArray.ToDictonary(

看这个方法需要的参数,第一个参数需要键和第二个参数需要值。我们知道Dictionary是一个泛型,它是键/值对类型的集合。因此,这里我们用一个lambda表达式传递Dictionary对象名称。

StudentArray.ToDictionary(key => key.Id,Studentobj => Studentobj); 

这个ToDictionary方法返回的类型是Dictionary 对象。 其键/值对<int,Student>类型,同样说明我们可以创建一个该类型的对象来存储ToDictionary方法得到的数据。

Dictionary<int, Student> StudentDictionary = StudentArray.ToDictionary(key => key.Id,Studentobj => Studentobj);

使用foreach从这个StudentDictionary对象遍历学生资料,如下:

foreach (KeyValuePair<int, Student> student in StudentDictionary)
{
Console.WriteLine("Id = "+student.Key+" "+" Name = "+student.Value.Name+" "+" Gender = "+student.Value.Gender);
}

运行程序

Dictionary转换为数组
将Dictionary转换成数组,使用ToArray扩展方法。在之前,需要获取Dictionary对象的集合中的值,所以我们使用Values属性的ToArray方法。

Student[] DictionaryToArray = StudentDictionary.Values.ToArray();

使用foreach遍历学生资料

foreach (Student student in DictionaryToArray)
{
Console.WriteLine("Id = "+student.Id+" "+" Name = " +student.Name+" "+" Gender = "+student.Gender);
}

运行程序

List转换为Dictionary

之前已经创建了一个StudentList学生对象,将StudentList转换为Dictionary我们调用ToDictionary方法。

Dictionary<int, Student> ListToDictionary = StudentList.ToDictionary(key => key.Id, value => value);

对于ToDictionary方法的两个参数,我们分别通过键和值传递其对象。这里ToDictionary被赋值,并返回了一个< int,Student >Dictionary 对象。所以我们创建该类型的对象然后存储返回的数据,最后用foreach获取学生资料。

foreach (KeyValuePair<int,Student> student in ListToDictionary)
{
Console.WriteLine("Id = "+student.Key+" "+" Name = " +student.Value.Name+" "+" Gender = "+student.Value.Gender);
}

运行程序

Dictionary转换为List

将Dictionary 转换成List调用ToList方法,之前已经创建了一个StudentDictionary对象。直接看如何这个对象转换到list.

List<Student> DictionaryToList = StudentDictionary.Values.ToList();
foreach (Student student in DictionaryToList)
{
Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender);
}

运行程序

IQueryable,IEnumerable,List相互转换

IQueryable,IEnumerable都可以通过ToList()转换为类型。

PassUser.ToList();

如果需要反向转换,有两个很好用的方法AsQueryable(),AsEnumerable(),可以顺利将List转换为IQueryable,IEnumerable。

List<MO> ListUser = new List<MO>();
PassUser = ListUser.AsQueryable();

希望本文对你有帮助

********转载:https://www.cnblogs.com/Yesi/p/6229522.html

C#数组,List,Dictionary,IQueryable,IEnumerable的相互转换的更多相关文章

  1. C#数组,List,Dictionary的相互转换

    本篇文章会向大家实例讲述以下内容: 将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dicti ...

  2. IQueryable,IEnumerable,List相互转换

    发个文记录一下犯的错误吧!!! 如果在使用ASP.NET MVC很多的数据存取都是以IQueryable<>泛型类接收,那么在做两个IQueryable<>集合拼接时对于新手可 ...

  3. Entity Framework中IQueryable, IEnumerable, IList的区别(转载)

    原文:http://www.cnblogs.com/hiteddy/archive/2011/10/01/Difference_among_IQueryable_IEnumeralb_IList_in ...

  4. 一个Json、数组、Dictionary转换和数组对比的C#实例

    最近做了一个程序,里面一段代码用到Json.数组.Dictionary转换和数组对比的一些知识,虽然在实际碰到类似问题时候有更好的方法,但这就当是一次基础知识的回顾,现在分享一下. 先介绍下要实现的业 ...

  5. Entity Framework中IQueryable, IEnumerable, IList的区别[转]

    使用工具追踪EF生成的SQL 使用Entity Framework等ORM框架的时候,SQL对于使用者来说是透明的,往往很多人也不关心ORM所生成的SQL,然而系统出现性能问题的时候就必须关注生成的S ...

  6. Swift3 - String 字符串、Array 数组、Dictionary 字典的使用

    Swift相关知识,本随笔为 字符串.数组.字典的简单使用,有理解.使用错误的地方望能指正. ///************************************************** ...

  7. delphi char数组、string和Pchar的相互转换

    因为要调用windows的api或者给vc++写接口,很多地方都要用到pchar,现在将char数组.string和pchar之间的相互转换都列出来,都是网上找的资料,我总结一下,先直接上代码,再讲原 ...

  8. IQueryable,IEnumerable,IList区别

    IQueryable和IEnumerable都是延时执行(Deferred Execution)的,而IList是即时执行(Eager Execution)IQueryable和IEnumerable ...

  9. JSON字符串和Dictionary字典类型的相互转换

    在开发过程中,往往会遇到数据类型转换的情况,根据自己的业务,可能转换类型有多种,下面就说一下json字符串和字典类型的转换. public static class JsonUntity { /// ...

随机推荐

  1. QuartJob的CronExpressionString规则详解

    字段 允许值 允许的特殊字符    秒   0-59   , - * /    分   0-59   , - * /    小时 0-23   , - * /    日期 1-31   , - * ? ...

  2. 回答了这四个问题,你就可以打造最佳App首页

    如果把手机APP比作人的话,首页就是脸面了.首页是一款产品的大门,好的开头就是成功的一半. 调查表示,26%的手机APP的平均使用次数只有一次.对首次使用产品的用户而言,首页的好坏关乎到用户对该产品的 ...

  3. sklearn中决策树算法DesiciontTreeClassifier()调用以及sklearn自带的数据包sklearn.datasets.load_iris()的应用

    决策树方法的简单调用记录一下 clf=tree.DecisionTreeClassifier() dataMat=[];labelMat=[] dataPath='D:/machinelearning ...

  4. 视觉SLAM的数学基础 第一篇 3D空间的位置表示

    视觉SLAM中的数学基础 第一篇 3D空间的位置表示 前言 转眼间一个学期又将过去,距离我上次写<一起做RGBD SLAM>已经半年之久.<一起做>系列反响很不错,主要由于它为 ...

  5. python的return

    关于python的return用法,在stackoverflow里的问题: Python — return, return None, and no return at all Consider th ...

  6. java并发编程实战:第十二章---并发程序的测试

    并发程序中潜在错误的发生并不具有确定性,而是随机的. 安全性测试:通常会采用测试不变性条件的形式,即判断某个类的行为是否与其规范保持一致 活跃性测试:进展测试和无进展测试两方面,这些都是很难量化的(性 ...

  7. php CI框架输出空行问题排查

    今天在使用 curl 命令行工具调试一个功能时,发现输出的内容总是会在最开始莫名其妙的多一行空行: 项目框架是 php 的 CodeIgniter,感觉这种问题在网上不好查找,因为可以确定这个是业务出 ...

  8. Android-Sqlite-升级操作

    一想到Android到数据库,只需要想到一个类 SQLiteOpenHelper,然后写一个类继承 SQLiteOpenHelper,重写构造方法,对数据库进行配置 public class MySQ ...

  9. C#基础入门 八

    C#基础入门 八 泛型 C#中的泛型能够将类型作为参数来传递,即在创建类型时用一个特定的符号,如"T"来作为一个占位符,代替实际的类型,等待实例化时用一个实际的类型来代替. pub ...

  10. [20190423]oradebug peek测试脚本.txt

    [20190423]oradebug peek测试脚本.txt --//工作测试需要写一个oradebug peek测试脚本,不断看某个区域内存地址的值. 1.环境: SCOTT@book> @ ...