If statement in LINQ

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
Im trying to make an application which shows the bushours including busnumbers, Aimed hour and Expecpted hour.
I get my (live) information from a httpwebRequest. My response from the request is stored in a string variable in XML format.
I can get all the information that I want; like the bus hour, Aimed hour and Expected hour.
The problem is that if there is no Expected hour nothnig will be showed. I like to have when there is no Expected hour my code just takes the same value as the Aimed hour:

An example

Bus Aimed Execepted

-----------------------------------------------------
1 17:05 17:07
2 17:05 <nothing> so take value of aimed -> 17:05

I have already the following code //XMLResponse put in documentRoot
//responseFromServer is a string variable in XML format with all the information
XElement documentRoot = XDocument.Parse(responseFromServer).Root;

XNamespace ns = "http://www.siri.org.uk/";


var buses = (from tblBuses in documentRoot.Descendants(ns + "PublishedLineName")
select tblBuses.Value).ToList();

var expHours = (from tblHours in documentRoot.Descendants(ns + "ExpectedDepartureTime")
select tblHours.Value).ToList();

foreach (var bus in buses)
{
string output = bus.Substring(bus.IndexOf(T) + 1);
int index = output.IndexOf(".");
if (index > 0)
output = output.Substring(0, index);
listBox1.Items.Add("Bus: " + output);
}


//Show every ExpectedDepartureTime
//If there is no expectedTime take value AimedDepartureTime
foreach (var expH in expHours)
{
string output = expH.Substring(expH.IndexOf(T) + 1);
int index = output.IndexOf(".");
if (index > 0)
output = output.Substring(0, index);
lstHours.Items.Add(output);
}
for being more clear to having an understand of my XML response, below an example of my XML response (One with AimedDeparturetime and Expected and one with witouth Expected)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Siri version="1.0" xmlns="http://www.siri.org.uk/
<ServiceDelivery>
<ResponseTimestamp>2013-03-26T16:09:48.181Z</ResponseTimestamp>
<StopMonitoringDelivery version="1.0
<ResponseTimestamp>2013-03-26T16:09:48.181Z</ResponseTimestamp>
<RequestMessageRef>12345</RequestMessageRef>

<MonitoredStopVisit>
<RecordedAtTime>2013-03-26T16:09:48.181Z</RecordedAtTime>
<MonitoringRef>020035811</MonitoringRef>
<MonitoredVehicleJourney>
<FramedVehicleJourneyRef>
<DataFrameRef>-</DataFrameRef>
<DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
</FramedVehicleJourneyRef>
<VehicleMode>bus</VehicleMode>
<PublishedLineName>2</PublishedLineName>
<DirectionName>Elstow P+R</DirectionName>
<OperatorRef>STB</OperatorRef>
<MonitoredCall>
<AimedDepartureTime>2013-03-26T16:11:00.000Z</AimedDepartureTime>
<ExpectedDepartureTime>2013-03-26T16:11:28.000Z</ExpectedDepartureTime>
</MonitoredCall>
</MonitoredVehicleJourney>
</MonitoredStopVisit>
---------------------------------------------------

<MonitoredStopVisit>
<RecordedAtTime>2013-03-26T16:09:48.181Z</RecordedAtTime>
<MonitoringRef>020035811</MonitoringRef>
<MonitoredVehicleJourney>
<FramedVehicleJourneyRef>
<DataFrameRef>-</DataFrameRef>
<DatedVehicleJourneyRef>-</DatedVehicleJourneyRef>
</FramedVehicleJourneyRef>
<VehicleMode>bus</VehicleMode>
<PublishedLineName>53</PublishedLineName>
<DirectionName>Wootton</DirectionName>
<OperatorRef>STB</OperatorRef>
<MonitoredCall>
<AimedDepartureTime>2013-03-26T16:19:00.000Z</AimedDepartureTime>
</MonitoredCall>
</MonitoredVehicleJourney>
</MonitoredStopVisit>
</StopMonitoringDelivery>
</ServiceDelivery>
</Siri>

So for this moment my application doesnt show every departuretime of a bus.
How can I solve this?
Thanks!

View the full article
 
Back
Top