linq to object query

Discussion in 'ASP .Net' started by kidders, May 7, 2010.

  1. kidders

    kidders Guest

    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 :)
     
    kidders, May 7, 2010
    #1
    1. Advertising

  2. kidders

    Mr. Arnold Guest

    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.
     
    Mr. Arnold, May 7, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ChrisN
    Replies:
    1
    Views:
    562
    Alexey Smirnov
    Aug 7, 2007
  2. shapper

    LINQ Query

    shapper, Oct 17, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    384
    sloan
    Oct 18, 2007
  3. Alex Sauceda

    SQL Query to Linq (Group By and Sum)

    Alex Sauceda, Feb 4, 2008, in forum: ASP .Net
    Replies:
    6
    Views:
    41,220
    Alex Sauceda
    Feb 4, 2008
  4. George

    Linq or not Linq

    George, Nov 4, 2008, in forum: ASP .Net
    Replies:
    4
    Views:
    384
    Mike Gleason jr Couturier
    Nov 5, 2008
  5. kidders

    linq to object query

    kidders, Feb 11, 2010, in forum: ASP .Net
    Replies:
    2
    Views:
    1,258
    kidders
    Feb 16, 2010
Loading...

Share This Page