zoukankan      html  css  js  c++  java
  • C# Freely convert between IList<T> and IEnumerable<T>

    项目中有一处用到将List<T>连接起来。可是在调用Concat方法后,连接后结果却转换为 IEnumerable<T>,如何将其转换回来?

    正在踌躇,忽然间一眼发现了IEnumerable接口竟然已经存在了转换方法:public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source);大喜!

    附,一示例:

    代码
    IList<Student> studentList1 = new List<Student>();
            Student s1   
    = new Student();
            s1.ID 
    = "1";
            s1.Name 
    = "张三";

            studentList1.Add(s1);

            IList
    <Student> studentList2 = new List<Student>();
            Student s2 
    = new Student();
            s2.ID 
    = "2";
            s2.Name 
    = "李四";

            studentList2.Add(s2);

            List
    <Student> xxx = studentList1.ToList();
            xxx.AddRange(studentList2.ToList());

            
    int c1 = xxx.Count;


            IList
    <Student> yyy = studentList1.Concat(studentList2).ToList<Student>();

            
    int c2 = yyy.Count;

    网上示例地址

    备注:在同事的提醒下,又发现一转换方法AddRange,并将其修改到示例代码中。

    文章出处:www.cnblogs.com/jizhong

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。否则保留追究法律责任的权利。

  • 相关阅读:
    mbed TLS 介绍
    PostGIS:Working with Raster Data
    TIN数据格式:DEM的三种表示方法之一
    ArcScene显示DEM
    Python与MapNik 等高线渲染&抽稀
    Android GPS定位
    osmdroid通过点击获取当前坐标
    osmdroid高级教程
    mongodb 用户 权限 设置 详解
    Mongodb设置用户权限(整理版)
  • 原文地址:https://www.cnblogs.com/jizhong/p/1879953.html
Copyright © 2011-2022 走看看