C# 获取 IEnumerable 集合的个数
IEnumerable<Attribute> keys = p.GetCustomAttributes().ToList();
var data1 = data.Where(n => n.Name.Contains(search)).ToList();
if (data1.Count == 0)
//MSDN例子
using System;
using System.Windows.Forms;
using System.Collections; namespace Exceltest
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
} private void Form3_Load(object sender, EventArgs e)
{
MessageBox.Show("");
Person[] peopleArray = new Person[]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
var ss = new People(peopleArray); //创建一个对象,这个对象_people属性是一个数组 People peopleList = new People(peopleArray);
foreach (Person p in peopleList) //如果不集成IEnumerable,对象将无法使用foreach
Console.WriteLine(p.firstName + " " + p.lastName);
}
} public class Person //人,个人
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
} //构造方法
public string firstName;
public string lastName;
} public class People : IEnumerable //人,大家
{
private Person[] _people; //定义一个个人数组 public People(Person[] pArray) //构造函数需要传递过来一个数组 /////数组传递到people中
{
_people = new Person[pArray.Length]; //初始化数组的范围, for (int i = ; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
} IEnumerator IEnumerable.GetEnumerator() // 集成必须要实现该接口
{
PeopleEnum ss = new PeopleEnum(_people); return new PeopleEnum(_people); //返回
}
} public class PeopleEnum : IEnumerator
{
public Person[] _people; // Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -; public PeopleEnum(Person[] list)
{
_people = list;
} public bool MoveNext()
{
position++;
return (position < _people.Length);
} public void Reset()
{
position = -;
} public object Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
}
C# 获取 IEnumerable 集合的个数的更多相关文章
- 【转载】 C#中使用Count方法获取List集合中符合条件的个数
很多时候操作List集合的过程中,我们需要根据特定的查询条件,获取List集合中有多少个实体对象符合查询条件,例如一批产品的对象List集合,如果这批产品的不合格数量大于10则重点备注.在C#中可以自 ...
- Atitit利用反射获取子类 集合 以及继承树
Atitit利用反射获取子类 集合 以及继承树 想从父类往下找子类的确是不可能的,要知道只要类不是final的话谁都有继承它的自由不需要事前通知父类. Eclipse实现不是重父类开始找而是重子类往回 ...
- Js获取后台集合List的值和下标的方法
Js获取后台集合List的值和下标的方法 转载自:http://blog.csdn.net/XiaoKanZheShiJie/article/details/47280449 首先用的是struts2 ...
- 从值栈获取List集合
-------------------siwuxie095 从值栈获取 List 集合 1.具体步骤 (1)在 Action 中向值栈放 List 集合 (2)在 JSP 页面中从值栈获取 List ...
- 反射方式,获取出集合ArrayList类的class文件对象
/* * 定义集合类,泛型String * 要求向集合中添加Integer类型 * * 反射方式,获取出集合ArrayList类的class文件对象 * 通过class文件对象,调用add方法 * * ...
- Java分享笔记:使用entrySet方法获取Map集合中的元素
/*--------------------------------- 使用entrySet方法取出Map集合中的元素: ....该方法是将Map集合中key与value的关系存入到了Set集合中,这 ...
- Unity3D_06_根据Transform、GameObject和Tag获取子对象集合
导引: 因为项目中难免要多次进行获取子对象或者子对象的集合,所以写一个单独的类,用来做这些操作.然后再实际的项目中,只需要使用 transform 或者 gameobject 调用这些方法就可以快速的 ...
- 获取单列集合,双列集合,数组的Stream流对象以及简单操作
获取流对象 获取单列集合,双列集合,数组的流对象 单列集合获取流对象: 1.java.util.Collection接口中加入了default方法stream()获取流对象,因此其所有实现类均可通过此 ...
- Selenium获取页面指定元素个数
测试需求: 获取页面中下拉框个数,并验证是否与预期个数一致 方法1:因下拉框的tagname属性值为select,可通过获取标签为select的元素来获取下拉框个数 List<WebElem ...
随机推荐
- 对SHH的公钥和私钥的简单理解
SSH是在应用层和传输层基础上的安全协议 SSH提供了两种级别的安全验证: 第一基于密码的安全验证:账号.密码,但可能有别的服务器冒充真正的服务器,无法避免被“中间人”攻击(man-in-the-mi ...
- Spring MVC 注解基础
@Controller @Controller 注解用于标记在 Java 类上.被 @Controller 标记过的类就是一个 SpringMVC Controller对象.DispatcherSer ...
- 洛谷 P2997 [USACO10NOV]旗帜Banner
P2997 [USACO10NOV]旗帜Banner 题目背景 题目大意(by:曹彦臣): 平面上有(0,0)到(n,m)的(n+1)*(m+1)个点.问有多少点对所连的线段不过其他点,且长度在[l, ...
- MYSQL 字符集 MYSQL 源码
http://blog.csdn.net/maray/article/details/46504621
- java 的collection
参考:http://skyuck.iteye.com/blog/526358 https://www.tutorialspoint.com/java/java_collections.htm Prio ...
- AVPlayer的使用,带缓冲
#import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...
- Android自己定义Toast
一.引言 在开发的过程中你会发现Android自身的Toast提示有很多限制,比方我想自己定义Toast的动画.自己定义一个美观的View显示在Toast中.很多其它的是让Toast显示指定的时长等等 ...
- 【Geforce】关于如何在Geforce Experience中登录
相信不少人无法登录这个该死的Geforce Experience.这里提供几个解决方案: 1.在“服务”中启动运行 NVIDIA NetworkService Container 方式改为手动或者自动 ...
- 使用playonlinux安装windows软件
转载 http://qspy.is-programmer.com/posts/40913.html Wine提供了一个用来运行Windows程序的平台.PlayOnLinux 是使用 Python 写 ...
- 请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码。
请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码. 2013-06-18 11:07会飞的鱼儿18 | 分类:C#/.NET | 浏览101次 不能有空 ...