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 ...
随机推荐
- 利用rsync如何同步单个文件
rsync -vzrtopg --progress --include-from=/home/test/include.list --exclude=/* root@192.168.30.179::b ...
- 最短路径--Floyd算法
Floyd算法 1.定义概览 Floyd-Warshall算法(Floyd-Warshall algorithm)是解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权的最短路径问题,同时也被 ...
- iOS: 将OC的NSMutableArray转为Swift的Array
一句话: YourSwiftObject convertedArray = your_MutableArray as NSArray as [YourSwiftObject]
- 从头认识Spring-2.7 自己主动检測Bean(1)-@Component @Repository @Service @Controller
这一章节我们来讨论一下自己主动检測Bean. 1.domain 厨师类: package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_19; ...
- pat解题报告【1082】
1082. Read Number in Chinese (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard ...
- 20170322Linux
- oc37--类工厂方法
// // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int ag ...
- 使用ALSA编写自己的音频程序【转】
本文转载自:http://blog.csdn.net/beyondioi/article/details/6994548 Alsa是Linux高级音频接口.面对众多的音频设备,Alsa为Linux音频 ...
- string[][]和string[,] 以及 int[][]和int[,]
string[][]和string[,] http://www.codewars.com/kata/56f3a1e899b386da78000732/train/csharp Write a func ...
- [Apple开发者帐户帮助]五、管理标识符(3)删除应用程序ID
您可以在不再需要时删除App ID.但是,您无法删除上载到App Store Connect的应用程序的显式应用程序ID . 所需角色:帐户持有人或管理员. 在“ 证书”,“标识符和配置文件”中,从左 ...