linq to object query

K

kidders

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 :)
 
M

Mr. Arnold

kidders said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top