Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. LINQ, parsing and error handling

LINQ, parsing and error handling

Scheduled Pinned Locked Moved LINQ
jsonquestioncsharplinqxml
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cesa37
    wrote on last edited by
    #1

    I am reading data from an XML using LINQ. Some elements contains data that I want to parse to a specific object type, say for example they are numbers that I want to parse to integers. If the data in one element is incorrect (i.e. "abc" where there should be a number) I just want to skip it and read the rest (meaning a try...catch around the entire LINQ would not work). If I were using loops I would just use a try...catch around the Parse method, with a continue; in the catch. But how do I accomplish this using LINQ? One solution is to use TryParse in a where statement, but then I have to parse the text twice, first with TryParse in the where, and then with Parse in select. Ok, parsing an integer might be cheap, but say: *There is no TryParse for the object I want, only a Parse method throwing exceptions (ok, I could write a TryParse wrapper) or *The parsing is not cheap for the specific object type I want, and it would be a major performance hog having to do it twice. How would I go about doing this?

    G 1 Reply Last reply
    0
    • C Cesa37

      I am reading data from an XML using LINQ. Some elements contains data that I want to parse to a specific object type, say for example they are numbers that I want to parse to integers. If the data in one element is incorrect (i.e. "abc" where there should be a number) I just want to skip it and read the rest (meaning a try...catch around the entire LINQ would not work). If I were using loops I would just use a try...catch around the Parse method, with a continue; in the catch. But how do I accomplish this using LINQ? One solution is to use TryParse in a where statement, but then I have to parse the text twice, first with TryParse in the where, and then with Parse in select. Ok, parsing an integer might be cheap, but say: *There is no TryParse for the object I want, only a Parse method throwing exceptions (ok, I could write a TryParse wrapper) or *The parsing is not cheap for the specific object type I want, and it would be a major performance hog having to do it twice. How would I go about doing this?

      G Offline
      G Offline
      Gideon Engelberth
      wrote on last edited by
      #2

      You could write a "ParseOrDefault" method that returns null when the parsing fails and check for null when you iterate the collection. Instead of the null check when you iterate, you could also use the Where method to filter the query results.

      YourObj ParseOrDefault(string s)
      {
      try
      {
      return YourObj.Parse(s);
      }
      catch (Exception e)
      {
      return null;
      }
      }

      C 1 Reply Last reply
      0
      • G Gideon Engelberth

        You could write a "ParseOrDefault" method that returns null when the parsing fails and check for null when you iterate the collection. Instead of the null check when you iterate, you could also use the Where method to filter the query results.

        YourObj ParseOrDefault(string s)
        {
        try
        {
        return YourObj.Parse(s);
        }
        catch (Exception e)
        {
        return null;
        }
        }

        C Offline
        C Offline
        Cesa37
        wrote on last edited by
        #3

        I had to think about your answer for a bit, you mean I should use something along the lines of

        let myObj = ParseOrDefault(s)
        where myObj != null

        Yes, that works fine. Thank you again.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups