博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IEnumerable、IEnumerator、IQuerabled的区别
阅读量:6946 次
发布时间:2019-06-27

本文共 1650 字,大约阅读时间需要 5 分钟。

1)为什么数组可以foreach遍历?

因为数组可以按需提供一个叫做枚举数(enumerator)的对象。该对象实现了IEnumerator接口。

提高一个层次说,所有被foreach遍历的对象实现了IEnumerable接口,在遍历时调用了该对象的GetEnumerator()反响。返回的是实现了 IEnumerator接口的对象。

枚举的三种形式:IEnumerable/IEnumerator形式、IEnumerable<T>/IEnumerator<T>泛型形式,不使用接口

也就是说,所有的数组默认实现了IEnemerator接口,包含三个方法 public object Current{get{return ;}} public bool MoveNext(){} public void Reset(){}

一个例子:

namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
MyColor colors = new MyColor();

foreach (var color in colors)

{
Console.WriteLine(color);
}

Console.Read();

}

}

class MyColor : IEnumerable
{
public IEnumerator GetEnumerator()
{
string[] Colors = { "Red", "yellow", "Blue" };
return new ColorEnumerator(Colors);
}
}

class ColorEnumerator : IEnumerator

{
String[] Colors;
private int Position = -1;
public object Current
{
get { return Colors[Position]; }
}

public bool MoveNext()

{
if (Position < Colors.Length - 1)
{
Position++;
return true;
}
return false;
}

public void Reset()

{
Position = -1;
}
public ColorEnumerator(string[] theColors)
{
Colors = new string[theColors.Length];
for (int i = 0; i < theColors.Length; i++)
{
Colors[i] = theColors[i];
}
}
}
}

 

2)linq to sql 查询

var q = from c in dbContext.Customers

where c.City == "shenzhen"
select c;

得到的结果是IQuerable类型,每次foreach时都得到数据库里拿。这样远程查询的好处是:可以利用数据库的索引,不会取到不用的数据。可以适用场景:foreach为1次,这样只需要查一次数据库。

而linq to object查询

var q = (from c in dbContext.Customers

where c.City == "shenzhen"
select c).AsEnumerable();
得到的结果则是IEnemerable<T>类型,每次foreach时,数据已经在本地的内存上了。适用场景: foreach次数多。

以上所有的foreach都可以替换为count(),sum()等聚合函数,执行这些函数时,已经对数据源进行查询了。

转载于:https://www.cnblogs.com/Benjamin/archive/2013/03/11/2954450.html

你可能感兴趣的文章
浅析设计模式(二)——工厂方法模式
查看>>
ubuntu设置开机开启小键盘[Linux]
查看>>
syq小姐姐的分享的历年考试经验
查看>>
linux 实践2.2 编译模块
查看>>
FAQs: 当在Outlook Explorer中右击邮件时,如何向上下文菜单添加按钮?(VSTO技术)...
查看>>
使用Java纯代码实现MySQL的连接
查看>>
面试宝典-面试题1
查看>>
javascript 数组排重
查看>>
DAY1 linux 50条命令
查看>>
http://hi.baidu.com/13655092904/blog
查看>>
Eclipse设置Tab键为四个空格
查看>>
CentOS7下安装mysql5.7
查看>>
Windows漏洞利用技术概述
查看>>
多态与接口
查看>>
HTML5标准学习 - 文档结构
查看>>
zookeeper练习
查看>>
最短路径
查看>>
paper 53 :深度学习(转载)
查看>>
数学基础-概率论05(统计推断-分布拟合检验)
查看>>
手机评测
查看>>