class Program
{
static readonly Person[] peopel =
{
new Person { Name="xhtmldivcss00", Age= 300 },
new Person { Name="xhtmldivcss01", Age= 301 },
new Person { Name="xhtmldivcss02", Age= 302 },
new Person { Name="xhtmldivcss03", Age= 303 },
new Person { Name="xhtmldivcss04", Age= 304 },
new Person { Name="xhtmldivcss05", Age= 305 },
new Person { Name="xhtmldivcss06", Age= 306 },
new Person { Name="xhtmldivcss07", Age= 307 },
new Person { Name="xhtmldivcss08", Age= 308 },
new Person { Name="xhtmldivcss09", Age= 309 },
new Person { Name="xhtmldivcss10", Age= 310 },
};
static void Main(string[] args)
{
IEnumerable<Person> group = peopel;
IEnumerable<Person> filtered1 = group.Where(p => p.Age > 308);
foreach (var p in filtered1)
Console.WriteLine("Name: {0}, Age: {1}", p.Name, p.Age);
IEnumerable<Person> filtered2 = group.Where(p => p.Name.Contains("7"));
Console.WriteLine("----------- filtered 2 ");
foreach( var p in filtered2)
Console.WriteLine("Name: {0}, Age: {1}", p.Name, p.Age);
Console.WriteLine("----------- filtered 3 ");
IEnumerable<Person> filtered3 = group.Where(p => p.Age < 303).ToArray();
foreach(var p in filtered3)
Console.WriteLine("Name: {0}, Age: {1}", p.Name, p.Age);
Console.WriteLine("----------- Array");
foreach(var p in filtered3)
Console.WriteLine("Name: {0}, Age: {1}", p.Name, p.Age);
Console.Read();
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
if (source is Iterator<TSource>)
{
return ((Iterator<TSource>) source).Where(predicate);
}
if (source is TSource[])
{
return new WhereArrayIterator<TSource>((TSource[]) source, predicate);
}
if (source is List<TSource>)
{
return new WhereListIterator<TSource>((List<TSource>) source, predicate);
}
return new WhereEnumerableIterator<TSource>(source, predicate);
}