• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

OfType的使用C#OfType的使用

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 

该关键字主要用在非泛型到泛型之间的转化,在有些场合还是很有用的;比如:在使用非泛型的时候,想使用LINQ表达式进行结果查询

如:声明非泛型集合

System.Collections.ArrayList fruits = new System.Collections.ArrayList(5);
fruits.Add("Mango");
fruits.Add("Orange");
fruits.Add("Apple");

fruits.Add(32);
fruits.Add("Banana");

若我们想得到含有“n”字母的水果,则使用如下方式

现将fruits转化为兼容 IEnumerable<T>的类型

var query = fruits.OfType<string>(); //只取出满足 string 的对象;

在使用LINQ表达式即可完成目的:

var result= from c in query where c.contain("n") select c;

今天突然在书上看到该关键字,感觉很有用就随笔记录下来了,仅供参考;

void Main()
{
    //請注意:ArrayList 並未實做 IEnumerable<T>,所以不能應用 LINQ
    ArrayList ary = new ArrayList();
    //加入三個顧客資料
    ary.Add(new Customer {Id = 1, Name = "Leo", Age = 36});
    ary.Add(new Customer {Id = 2, Name = "Rose", Age = 28});
    ary.Add(new Customer {Id = 3, Name = "Alvin", Age = 2});
    //加入三個顧客的訂單資料
    ary.Add(new Order {CustomerId = 3, OrderDate = new DateTime(2011, 10, 9), Total = 2940});
    ary.Add(new Order {CustomerId = 2, OrderDate = new DateTime(2012, 10, 10), Total = 3849});
    ary.Add(new Order {CustomerId = 1, OrderDate = new DateTime(2011, 12, 1), Total = 500});
    ary.Add(new Order {CustomerId = 1, OrderDate = new DateTime(2012, 2, 28), Total = 1234});
    ary.Add(new Order {CustomerId = 2, OrderDate = new DateTime(2012, 5, 20), Total = 9520});
    //透過 OfType 過濾出訂單資料,並用 Where 條件查出小於千元的訂單
    var queryOrder = from e in ary.OfType<Order>()
                where e.Total < 1000
                select e;
    foreach (var e in queryOrder)
    {
        Console.WriteLine(e.ToString());
    }
}
//顧客基本資料類別
public class Customer
{
    public int Id {get; set;}
    public string Name {get; set;}
    public int Age { get; set; }
    
    public override string ToString()
    {
        return string.Format("Id = {0}, Name = {1}, Age = {2}", 
                                Id, Name, Age);
    }
}
//訂單基本資料類別
public class Order
{
    public int CustomerId { get; set; }
    public DateTime OrderDate { get; set; }
    public double Total { get; set; }
    
    public override string ToString()
    {
        return string.Format("CustomerId = {0}, OrderDate = {1}, Total = {2}", 
                                CustomerId, OrderDate, Total);
    }
}
//輸出:
//CustomerId = 1, OrderDate = 2011/12/1 上午 12:00:00, Total = 500

 

该关键字主要用在非泛型到泛型之间的转化,在有些场合还是很有用的;比如:在使用非泛型的时候,想使用LINQ表达式进行结果查询

如:声明非泛型集合

System.Collections.ArrayList fruits = new System.Collections.ArrayList(5);
fruits.Add("Mango");
fruits.Add("Orange");
fruits.Add("Apple");

fruits.Add(32);
fruits.Add("Banana");

若我们想得到含有“n”字母的水果,则使用如下方式

现将fruits转化为兼容 IEnumerable<T>的类型

var query = fruits.OfType<string>(); //只取出满足 string 的对象;

在使用LINQ表达式即可完成目的:

var result= from c in query where c.contain("n") select c;

今天突然在书上看到该关键字,感觉很有用就随笔记录下来了,仅供参考;

void Main()
{
    //請注意:ArrayList 並未實做 IEnumerable<T>,所以不能應用 LINQ
    ArrayList ary = new ArrayList();
    //加入三個顧客資料
    ary.Add(new Customer {Id = 1, Name = "Leo", Age = 36});
    ary.Add(new Customer {Id = 2, Name = "Rose", Age = 28});
    ary.Add(new Customer {Id = 3, Name = "Alvin", Age = 2});
    //加入三個顧客的訂單資料
    ary.Add(new Order {CustomerId = 3, OrderDate = new DateTime(2011, 10, 9), Total = 2940});
    ary.Add(new Order {CustomerId = 2, OrderDate = new DateTime(2012, 10, 10), Total = 3849});
    ary.Add(new Order {CustomerId = 1, OrderDate = new DateTime(2011, 12, 1), Total = 500});
    ary.Add(new Order {CustomerId = 1, OrderDate = new DateTime(2012, 2, 28), Total = 1234});
    ary.Add(new Order {CustomerId = 2, OrderDate = new DateTime(2012, 5, 20), Total = 9520});
    //透過 OfType 過濾出訂單資料,並用 Where 條件查出小於千元的訂單
    var queryOrder = from e in ary.OfType<Order>()
                where e.Total < 1000
                select e;
    foreach (var e in queryOrder)
    {
        Console.WriteLine(e.ToString());
    }
}
//顧客基本資料類別
public class Customer
{
    public int Id {get; set;}
    public string Name {get; set;}
    public int Age { get; set; }
    
    public override string ToString()
    {
        return string.Format("Id = {0}, Name = {1}, Age = {2}", 
                                Id, Name, Age);
    }
}
//訂單基本資料類別
public class Order
{
    public int CustomerId { get; set; }
    public DateTime OrderDate { get; set; }
    public double Total { get; set; }
    
    public override string ToString()
    {
        return string.Format("CustomerId = {0}, OrderDate = {1}, Total = {2}", 
                                CustomerId, OrderDate, Total);
    }
}
//輸出:
//CustomerId = 1, OrderDate = 2011/12/1 上午 12:00:00, Total = 500

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
在c#中IO流读写操作发布时间:2022-07-13
下一篇:
转载——利用C#自带组件强壮程序日志利用C#自带组件强壮程序日志 ...发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap