If statement in LINQ (If no value do something...) - c#

I'm trying to make an application which shows the bushours including bus numbers, Aimed hour and Expected 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 nothing 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 without 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 doesn't show every departure time of a bus.
How can I solve this?
Thanks!

Apologies upfront, because this is not the greatest XML parsing ever, but I would adjust my LINQ query:
var buses = from tblBuses in documentRoot.Descendants(ns + "MonitoredVehicleJourney")
select new
{
LineName = tblBuses.Descendants(ns + "PublishedLineName").Single().Value,
AimedHours = tblBuses.Descendants(ns + "AimedDepartureTime").Single().Value,
ExpectedHours = tblBuses.Descendants(ns + "ExpectedDepartureTime").Select(el => el.Value).SingleOrDefault()
};
This will create an IEnumerable of some anonymous type which allows you to access the bus data more easily in subsequent code:
foreach (var bus in buses)
{
// Take ExpectedHours, or AimedHours if the first is null
string expH = bus.ExpectedHours ?? bus.AimedHours
// Same code as before here
string output = expH.Substring(expH.IndexOf('T') + 1);
int index = output.IndexOf(".");
if (index > 0)
output = output.Substring(0, index);
lstHours.Items.Add(output);
}
In your original code, buses that did not have an <ExpectedDepartureTime> were never iterated over because they never show up in your expHours List. In contrast, this LINQ query will contain all buses. It assumes that they all have a single <AimedDepartureTime> and an optional <ExpectedDepartureTime>.
For the expected departure time, I used a Select to get the element value for each of the descendants. Using SingleOrDefault().Value cannot be used, because the query might yield no elements and get_Value() would be called on a null reference.
One last comment about my query: for production code I would refrain from using Descendants and do more strict querying of the XML structure.

You can do everything inside one linq query and use an ?: conditional operator to select the correct output:
var buses =
(from tblBuses in documentRoot.Descendants(ns + "PublishedLineName")
let bus = tblBuses.Value
let output = bus.Substring(bus.IndexOf('T') + 1)
let index = output.IndexOf(".")
select (index > 0) ? output.Substring(0, index) : output);
foreach (var bus in buses)
{
listBox1.Items.Add("Bus: " + bus);
}
or even
var buses =
(from ...
select "Bus: " + ((index > 0) ? output.Substring(0, index) : output));
.ToArray();
listBox1.Items.AddRange(buses);
The same pattern can be applied to expHours.

Related

How to do Azure search for text containing quotes(escape single quote)?

I have a Query for azure search as given below
results = indexClient.Documents.Search<Hotel>("", new SearchParameters { IncludeTotalResultCount = true, Filter = "(Provider eq 'Auction.com' or Provider eq 'Zeroiron' or Provider eq 'Gilbert'sYard')" });
the current Query Gives error because as i have given every provider inside quotes , but Gilbert's Yard already have a Quote inside the provider name itself, so to search for same Query with "Gilbert's yard" what change i have to make in the Query?
The above Query is generated like this,
var selectedProviders = this.Providers.Where(i => i.IsSearchable).ToList();
if (selectedProviders.Count > 0)
{
if (filterString.Length > 0)
filterString.Append(" and ");
filterString.Append("(");
var count = 1;
foreach (var provider in selectedProviders)
{
filterString.Append(($"Provider eq '{provider.ProviderName}'"));
if (count < selectedProviders.Count)
{
filterString.Append(" or ");
}
count++;
};
filterString.Append(")");
}
And how i should change my Code here?
To achieve this we need to replace single quote with two single quotes.
filterString.Append(($"Provider eq '{provider.ProviderName.replace("'","''")}'"));
this will do the trick for me.

CRM Linq and Attribute Value

I have a quick question regarding LINQ and attributes in CRM. I am having some issue with this little piece of code that I am attempting to write.
//Create query to get the related regular rates
var rateres = from r in ServiceContext.CreateQuery("pafo_rate")
where r["pafo_assignmentid"].Equals(entity.Id)
select r;
//Exception Rate - excluding pto
// var eateres = from er in ServiceContext.CreateQuery("pafo_exceptionrate")
// where er["pafo_assignmentid"].Equals(entity.Id) && er["pafo_assignmentid"] != ""
// select er;
foreach (var r in rateres)
{
Entity e = (Entity)r;
// e["pafo_billrate"] = newbillupdate + payupdate;
// test = (decimal)e.Attributes["pafo_payrate"]; \\ <-- How do you fix?
// test = 6;
e["pafo_payrate"] = (newpayupdate + test);
// ServiceContext.Attach(e);
ServiceContext.UpdateObject(e);
ServiceContext.SaveChanges();
}
When the plugin fires I get a message along the lines of:
Microsoft.Xrm.Sdk.SaveChangesException: An error occured while
processing this request.
I think it has to do with the way I am assigning to variable. Any help would be greatly appreciated!

XML linq need detail info on exception

I am using xml linq on my project. I am dealing with very large xml's for easy understanding purpose I have mentioned small sample xml.
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<StackOverflowReply xmlns="http://xml.stack.com/RRAND01234">
<processStatus>
<statusCode1>P</statusCode1>
<statusCode2>P</statusCode2>
<statusCode3>P</statusCode3>
<statusCode4>P</statusCode4>
</processStatus>
</StackOverflowReply>
</soap:Body>
Following is C# xml linq
XNamespace x = "http://xml.stack.com/RRAND01234";
var result = from StackOverflowReply in XDocument.Parse(Myxml).Descendants(x + "Security_AuthenticateReply")
select new
{
status1 = StackOverflowReply.Element(x + "processStatus").Element(x + "statusCode1").Value,
status2 = StackOverflowReply.Element(x + "processStatus").Element(x + "statusCode2").Value,
status3 = StackOverflowReply.Element(x + "processStatus").Element(x + "statusCode3").Value,
status4 = StackOverflowReply.Element(x + "processStatus").Element(x + "statusCode4").Value,
status5 = StackOverflowReply.Element(x + "processStatus").Element(x + "statusCode5").Value,
};
Here I am getting exception like "Object reference not set to an instance of an object.". Because the tag
<statusCode5>
was not in my xml.In this case I want to get detail exception message like "Missing tag statusCode5". Please guide me how to get this message from my exception.
There's no easy way (that I'm aware of) to find out exactly what element(s) was/were missing in a LINQ to XML statement. What you can do however is use (string) on the element to handle missing elements - but that can get tricky if you have a chain of elements.
That wouldn't work in your current code:
status5 = (string)StackOverflowReply.Element(x + "processStatus").Element(x + "statusCode5")
Becuase (string) will only work on first element, and the second one is the one that is missing.
You could change your LINQ to focus only on the subnodes, like this:
XNamespace x = "http://xml.stack.com/RRAND01234";
var result = from StackOverflowReply in XDocument.Parse(Myxml).Descendants(x + "processStatus")
select new
{
status1 = (string)StackOverflowReply.Element(x + "statusCode1"),
status2 = (string)StackOverflowReply..Element(x + "statusCode2"),
status3 = (string)StackOverflowReply..Element(x + "statusCode3"),
status4 = (string)StackOverflowReply.Element(x + "statusCode4"),
status5 = (string)StackOverflowReply.Element(x + "statusCode5"),
};
However, if your XML is complex and you have different depths (nested elements), you'll need a more robust solution to avoid a bunch of conditional operator checks or multiple queries.
I have something that might help if that is the case - I'll have to dig it up.
EDIT For More Complex XML
I've had similar challenges with some XML I have to deal with at work. In lieu of an easy way to determine what node was the offending node, and not wanting to have hideously long ternary operators, I wrote an extension method that worked recursively from the specified starting node down to the one I was looking for.
Here's a somewhat simple and contrived example to demonstrate.
<SomeXML>
<Tag1>
<Tag1Child1>Value1</Tag1Child1>
<Tag1Child2>Value2</Tag1Child2>
<Tag1Child3>Value3</Tag1Child3>
<Tag1Child4>Value4</Tag1Child4>
</Tag1>
<Tag2>
<Tag2Child1>
<Tag2Child1Child1>SomeValue1</Tag2Child1Child1>
<Tag2Child1Child2>SomeValue2</Tag2Child1Child2>
<Tag2Child1Child3>SomeValue3</Tag2Child1Child3>
<Tag2Chidl1Child4>SomeValue4</Tag2Child1Child4>
<Tag2Child1>
<Tag2Child2>
<Tag2Child2Child1>
<Tag2Child2Child1Child1 />
<Tag2Child2Child1Child2 />
</Tag2Child2>
</Tag2>
</SomeXML>
In the above XML, I had no way of knowing (prior to parsing) if any of the children elements were empty, so I after some searching and fiddling I came up with the following extension method:
public static XElement GetChildFromPath(this XElement currentElement, List<string> elementNames, int position = 0)
{
if (currentElement == null || !currentElement.HasElements)
{
return currentElement;
}
if (position == elementNames.Count - 1)
{
return currentElement.Element(elementNames[position]);
}
else
{
XElement nextElement = currentElement.Element(elementNames[position]);
return GetChildFromPath(nextElement, elmenentNames, position + 1);
}
}
Basically, the method takes the XElement its called on, plus a List<string> of the elements in path order, with the one I want as the last one, and a position (index in the list), and then works it way down the path until it finds the element in question or runs out of elements in the path. It's not as elegant as I would like it to be, but I haven't had time to refactor it any.
I would use it like this (based on the sample XML above):
MyClass myObj = (from x in XDocument.Parse(myXML).Descendants("SomeXML")
select new MyClass() {
Tag1Child1 = (string)x.GetChildFromPath(new List<string>() {
"Tag1", "Tag1Child1" }),
Tag2Child1Child4 = (string)x.GetChildFromPath(new List<string>() {
"Tag2", "Tag2Child1", "Tag2Child1Child4" }),
Tag2Child2Child1Child2 = (string)x.GetChildFromPath(new List<string>() {
"Tag2", "Tag2Child2", "Tag2Child2Child1",
"Tag2Child2Child1Child2" })
}).SingleOrDefault();
Not as elegant as I'd like it to be, but at least it allows me to parse an XML document that may have missing nodes without blowing chunks. Another option was to do something like:
Tag2Child2Child1Child1 = x.Element("Tag2") == null ?
"" : x.Element("Tag2Child2") == null ?
"" : x.Element("Tag2Child2Child1") == null ?
"" : x.Element("Tag2Child2Child1Child2") == null ?
"" : x.Element("Tag2")
.Element("Tag2Child2")
.Element("Tag2Child2Child1")
.Element("Tag2Child2Child1Child2").Value
That would get really ugly for an object that had dozens of properties.
Anyway, if this is of use to you feel free to use/adapt/modify as you need.

Parsing an UN XML file in C#

I'm trying to parse an XML file from UN website (http://www.un.org/sc/committees/1267/AQList.xml) using c#.
There is one problem I'm constantly having with this file, and that's the number of child tags varies from one <.INDIVIDUAL.> tag to another. One example is <.FORTH_NAME.> child tag.
I've tried a number of different approaches, but somehow I always seem to be stuck with the same problem, and that's different number of child tags inside <.INDIVIDUAL.> tag.
What I'm trying to achieve is to collect all the tags and their values under one <.INDIVIDUAL.> tag, and then insert only those I want into my database. If a tag is missing, for example <.FOURTH_NAME.>, than I need to insert only first three names into the database, and skip the fourth.
I've tried using Linq to XML, and here are some examples:
XDocument xdoc = XDocument.Load(path);
var tags = (from t in xdoc.Descendants("INDIVIDUALS")
from a in t.Elements("INDIVIDUAL")
select new
{
Tag = a.Name,
val = a.Value
});
foreach (var obj in tags)
{
Console.WriteLine(obj.Tag + " - " + obj.val + "\t");
//insert SQL goes here
}
or:
but this one only collects non empty FOURTH_NAME tags...
var q = (from c in xdoc.Descendants("INDIVIDUAL")
from _1 in c.Elements("FIRST_NAME")
from _2 in c.Elements("SECOND_NAME")
from _3 in c.Elements("THIRD_NAME")
from _4 in c.Elements("FOURTH_NAME")
where _1 != null && _2 != null && _3 != null && _4 != null
select new
{
_1 = c.Element("FIRST_NAME").Value,
_2 = c.Element("SECOND_NAME").Value,
_3 = c.Element("THIRD_NAME").Value,
_4 = c.Element("FOURTH_NAME").Value
});
foreach (var obj in q)
{
Console.WriteLine("Person: " + obj._1 + " - " + obj._2 + " - " + obj._3 + " - " + obj._4);
//insert SQL goes here
}
Any ideas??
Instead of calling Value on the element, consider using a string cast. LINQ to XML safely returns null if the element doesn't exist. Try the following:
var data = XElement.Load(#"http://www.un.org/sc/committees/1267/AQList.xml");
var individuals = data.Descendants("INDIVIDUAL")
.Select(i => new {
First = (string)i.Element("FIRST_NAME"),
Middle = (string)i.Element("SECOND_NAME"),
Last = (string)i.Element("THIRD_NAME")
});
If you want to be more flexible and get all of the name fields, you can do something like the following. (I'll leave the process of grouping individuals as an additional homework assignment ;-)
data.Descendants("INDIVIDUAL").Elements()
.Where (i =>i.Name.LocalName.EndsWith("_NAME" ))
.Select(i => new { FieldName= i.Name.LocalName, Value=i.Value});
Why don't you use XmlSerializer and LINQ instead ?
As explained in this answer, generate your classes by pasting in a new CS file :
menu EDIT > Paste Special > Paste XML As Classes.
Then grab your data as easily as follows :
var serializer = new XmlSerializer(typeof (CONSOLIDATED_LIST));
using (FileStream fileStream = File.OpenRead(#"..\..\aqlist.xml"))
{
var list = serializer.Deserialize(fileStream) as CONSOLIDATED_LIST;
if (list != null)
{
var enumerable = list.INDIVIDUALS.Select(s => new
{
FirstName = s.FIRST_NAME,
SecondName = s.SECOND_NAME,
ThirdName = s.THIRD_NAME,
FourthName = s.FOURTH_NAME
});
}
}
You can then specify any predicate that better suits your needs.
Going this path will be a huge time-saver and less error-prone, no need to use strings to access fields, strong typing etc ...

Parsing Xml file with namespace and sorting values in descending order

I am trying to parse the following xml file : http://reports.ieso.ca/public/DispUnconsHOEP/PUB_DispUnconsHOEP_20110714.xml
My goal is to get the maximum price with its associated hour and the minimum price with its associated hour.
The logic that I am applying is as follows:
Parse the Xml file and pass it to a IList which contains pairs of hour and price
Sort the IList in Descending order based on the price
Retrieve the first value in the IList as the maximum value whereas the last value in the IList as the minimum value.
My Code is:
XNamespace nsPriceHr = "http://www.theIMO.com/schema";
XDocument xDocument =
XDocument.Load("http://reports.ieso.ca/public/DispUnconsHOEP/PUB_DispUnconsHOEP_20110714.xml");
XElement xEl1 = xDocument.Element(nsPriceHr + "IMODocument");
XElement xEl2 = xEl1.Element(nsPriceHr + "IMODocBody");
XElement xEl3 = xEl2.Element(nsPriceHr + "HOEPs");
var data = (from x in xEl3.Descendants(nsPriceHr + "HOEP")
orderby x.Element(nsPriceHr + "Price").Value descending
select new HourPrice
{
Hour = x.Element(nsPriceHr + "Hour").Value,
Price = x.Element(nsPriceHr + "Price").Value
})
.ToList();
My Problem is: the List is not getting sorted as expected. The HourPrice object which takes the two values of Hour and Price has both data members as string.
I am using C#, .NET 3.5 SP1 and working on winforms. Any help appreciated
Try using System.Convert. In particular, you may be interested in ToInt32 and ToDecimal. These functions take a string (or one of various other data types), and convert it to an int or a decimal respectively.
var data = (from x in xEl3.Descendants(nsPriceHr + "HOEP")
orderby System.Convert.ToDecimal(x.Element(nsPriceHr + "Price").Value) descending
select new HourPrice
{
Hour = System.Convert.ToInt32(x.Element(nsPriceHr + "Hour").Value),
Price = System.Convert.ToDecimal(x.Element(nsPriceHr + "Price").Value)
})
.ToList();
Edit:
Here is one way you might check for missing values:
string valueString = x.Element("ElementName").Value;
int value = String.IsNullOrEmpty(valueString) ? 0 : Convert.ToInt32(valueString);
If your problem is that the strings aren't empty, but aren't in the right format either, you will have to convert them to the right format before you convert. I.e., you must strip the $ from a string like $6.47.

Categories

Resources