C#数组,List,Dictionary,IQueryable,IEnumerable的相互转换

本篇文章会向你们实例讲述如下内容: javascript

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

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

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

将数组转换为Listjava

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

复制代码
static void Main (string[] args) 
        {
            //建立数组
            Student[] StudentArray = new Student[3];
            //建立建立3个student对象,并赋值给数组的每个元素            StudentArray[0] = new Student()
            {
                Id = 203,
                Name ="Tony Stark",
                Gender ="Male"
            };
            StudentArray[1] = new Student()
            {
                Id = 205,
                Name="Hulk",
                Gender = "Male"
            };
            StudentArray[2] = new Student() 
            {
                Id = 210,
                Name ="Black Widow",
                Gender="Female"
            };
复制代码

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

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

运行程序this

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

注意这个ToList方法的返回类型,它返回的是List< Student >对象,这说明咱们能够建立一个该类型的对象来保存ToList方法返回的数据。3d

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

使用foreach从StudentList中获取全部的学生资料。code

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

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

运行程序htm

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

相关文章
相关标签/搜索