ToArray、ToList、ToDictionary、OfType

using #region using
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
#endregion


public  partial  class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
用ToArray()将查询结果转化为数组#region 用ToArray()将查询结果转化为数组
        Response.Write(
"<hr>用ToArray将查询结果转化位数组<br>");
        var students 
= from student in GetStudents()
                       orderby student.Age
                       select student.Name;
        var nameArray 
= students.ToArray();
        
foreach (var name in nameArray)
        
{
            Response.Write(
string.Format("<span class='result'>{0}</span>", name));
        }

        
#endregion
 

        
用ToList()将查询结果转化为List集合#region 用ToList()将查询结果转化为List集合
        Response.Write(
"<hr>用ToList()将查询结果转化为List集合<br>");
        var students1 
= from student in GetStudents()
                       where student.Age
>21
                       orderby student.Age
                       select student.Name;
        var nameArray1 
= students1.ToList();
        nameArray1.Add(
"newlist");
        
foreach (var name in nameArray1)
        
{
            Response.Write(
string.Format("<span class='result'>{0}</span>", name));
        }

        
#endregion
 

        
用ToDictionary()将查询结果转化为Dictionary#region 用ToDictionary()将查询结果转化为Dictionary
        Response.Write(
"<hr>用ToDictionary()将查询结果转化为Dictionary<br>");
        var studentDict 
= GetStudents().ToDictionary(student => student.Name);
        var Y 
= studentDict["YOUNG"];
        Response.Write(
string.Format("<span class='result'>YOUNG:{0},{1},{2}</span>", Y.Name, Y.Age, Y.Language));
        
#endregion
 

        
用OfType过滤查询结果中的类型#region 用OfType过滤查询结果中的类型
        Response.Write(
"<hr>用OfType过滤查询结果中的类型<br>");
        
object[] objects = 1"love",GetStudents()[1], 2.1null, GetStudents()[3],"young"};
        var students2 
= objects.OfType<Student>();
        
foreach (var student in students2)
        
{
            Response.Write(
string.Format("<span class='result'>{0}</span>", student.Name));
        }

        
#endregion

    }


    
构造一个学生集合体#region 构造一个学生集合体
    
private List<Student> GetStudents()
    
{
        List
<Student> students = new List<Student> 
             
new Student{ Name="YOUNG", Age=25, Language="Chinese"},
             
new Student{ Name="JESSIE", Age=21, Language="Scotland"},
             
new Student{ Name="KELLY", Age=18, Language="English"},
             
new Student{ Name="JUNE", Age=20, Language="English"},
             
new Student{ Name="ADRIAN", Age=22, Language="Italy"},
             
new Student{ Name="BRUCE", Age=17, Language="Scotland"},
             
new Student{ Name="BRANT", Age=30, Language="Germany"},
             
new Student{ Name="BEN", Age=25, Language="Chinese"}
        }
;
        
return students;
    }

    
#endregion

}


学生类 #region 学生类
class Student
{
    
public int Age getset; }
    
public string Name getset; }
    
public string Language getset; }
}

#endregion

显示结果
 

转载于:https://www.cnblogs.com/lihuimingcn/archive/2008/07/25/1251350.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值