Saturday, 31 August 2013

How to return IEnumerable instead of string with conditions in foreach?

How to return IEnumerable instead of string with conditions in foreach?

What approach do I have to make, to make something like the method below
to work for IEnumerable<Flight> instead of string? I want to manipulate my
IEnumerable source(in parameter) with conditions written in method, and
return IEnumerable based on those conditions only.
public string FilterFlights(IEnumerable<Flight> flights)
{
string s = "";
foreach (var flight in flights)
{
var indexItem = 0;
DateTime previousArrivalDateTime = new DateTime();
TimeSpan timeSpan;
int time = 0;
foreach (var segments in flight.Segments)
{
if (indexItem == 0)
{
previousArrivalDateTime = segments.ArrivalDate;
s = s + "Departure: " + segments.DepartureDate + ",
Arrival: " + segments.ArrivalDate + "; ";
}
if (indexItem > 0)
{
timeSpan = segments.DepartureDate - previousArrivalDateTime;
time += timeSpan.Hours;
s = s + "Departure: " + segments.DepartureDate + ",
Arrival: " + segments.ArrivalDate + "; ";
previousArrivalDateTime = segments.ArrivalDate;
}
indexItem++;
}
if (time > 2)
Console.WriteLine(s);
}
return s;
}
Thank you!

No comments:

Post a Comment