From: kidders on
Hi guys,

I'm trying to work out how to replicate the following in linq. I have
most of it the bit im stuck on is how to implement a try catch, and
that if an exception is caught the object doesnt get added to the
collection.

original code

List<ParkHireType> result = new List<ParkHireType>();
foreach (XElement node in
availableHire.Root.Elements("RENTAVAILABILITY"))
{
try
{
if (node.Attribute("code").Value == OfferCode)
{
result.Add(ParkHireType.Load(node));
}
}
catch (ParkHireTypeException)
{
//do something
}
}
return result;

linq so far:

return availableHire.Root.Elements("RENTALAVAILABILITY")
.Where(node =>
node.Elements("OFFER").Attributes("code").ElementAt(0).Value ==
OfferCode)
.Select(node => ParkHireType.Load(node))
.ToList<ParkHireType>();

thanks for any help :)
From: Mr. Arnold on
kidders wrote:
> Hi guys,
>
> I'm trying to work out how to replicate the following in linq. I have
> most of it the bit im stuck on is how to implement a try catch, and
> that if an exception is caught the object doesnt get added to the
> collection.
>
> original code
>
> List<ParkHireType> result = new List<ParkHireType>();
> foreach (XElement node in
> availableHire.Root.Elements("RENTAVAILABILITY"))
> {
> try
> {
> if (node.Attribute("code").Value == OfferCode)
> {
> result.Add(ParkHireType.Load(node));
> }
> }
> catch (ParkHireTypeException)
> {
> //do something
> }
> }
> return result;
>
> linq so far:
>
> return availableHire.Root.Elements("RENTALAVAILABILITY")
> .Where(node =>
> node.Elements("OFFER").Attributes("code").ElementAt(0).Value ==
> OfferCode)
> .Select(node => ParkHireType.Load(node))
> .ToList<ParkHireType>();
>
> thanks for any help :)

I think you're going to have to do query and hold the results in a
collection. Maybe using a Linq 'new shape' as output of objects.

Then you walk the collection of objects with a foreach loop adding
things to a List<T> as the returned results.


You're not going to be able to do anything with a try/catch and a Linq
query, other than, successful completion or abort out of the query.