Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > linq to object query

Reply
Thread Tools

linq to object query

 
 
kidders
Guest
Posts: n/a
 
      05-07-2010
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").ElementA t(0).Value ==
OfferCode)
.Select(node => ParkHireType.Load(node))
.ToList<ParkHireType>();

thanks for any help
 
Reply With Quote
 
 
 
 
Mr. Arnold
Guest
Posts: n/a
 
      05-07-2010
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").ElementA t(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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linq to object query kidders ASP .Net 2 02-16-2010 12:32 PM
Linq or not Linq George ASP .Net 4 11-05-2008 04:53 PM
SQL Query to Linq (Group By and Sum) Alex Sauceda ASP .Net 6 02-04-2008 10:26 PM
LINQ Query shapper ASP .Net 2 10-18-2007 02:59 PM
Please translate this C# LINQ query into VB for me !!!!! ChrisN ASP .Net 1 08-07-2007 07:29 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57