D
Dilip
Given a bunch of IEnumerable<T> objects, how do I iterate through them
and do a yield return on objects that satisfy one condition and
objects that satisfy another condition? I am still wrapping my head
over C# 2.0 iterators and I am not able to get this right.
public static IEnumerable<T> GetObjects(IEnumerable<T> objects)
{
foreach(T x in objects)
{
yield return x;
}
}
that obviously returns everything. I went the Predicate<T> route and
that got me here:
public static IEnumerable<T> GetObjects(IEnumerable<T> objects,
Predicate<T> condition)
{
foreach(T x in objects)
{
if (condition(x))
yield return x;
}
}
This works but it only returns objects that satisfy one condition.
What if I want all objects that return false too but on a different
call to GetObjects()?
Basically I am trying to find a way to use the same GetObjects()
function and vary the condition from the outside.
I can only work within the confines of .NET 2.0 (so no mindnumbing
LINQ suggestions )
thanks!
and do a yield return on objects that satisfy one condition and
objects that satisfy another condition? I am still wrapping my head
over C# 2.0 iterators and I am not able to get this right.
public static IEnumerable<T> GetObjects(IEnumerable<T> objects)
{
foreach(T x in objects)
{
yield return x;
}
}
that obviously returns everything. I went the Predicate<T> route and
that got me here:
public static IEnumerable<T> GetObjects(IEnumerable<T> objects,
Predicate<T> condition)
{
foreach(T x in objects)
{
if (condition(x))
yield return x;
}
}
This works but it only returns objects that satisfy one condition.
What if I want all objects that return false too but on a different
call to GetObjects()?
Basically I am trying to find a way to use the same GetObjects()
function and vary the condition from the outside.
I can only work within the confines of .NET 2.0 (so no mindnumbing
LINQ suggestions )
thanks!