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. Other Discussions
  3. The Weird and The Wonderful
  4. Sometimes, I'm too clever for my own good...

Sometimes, I'm too clever for my own good...

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasecsharplinqhelpquestion
12 Posts 7 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.
  • OriginalGriffO OriginalGriff

    It's official - I have too many eBooks to cope with without help, so I decided to knock up a quick SQL DB app to organise them (and play with creating a new DataGridView based UserControl with automatic fuzzy filtering). So I've just spent the last hour or so wondering where my data is / was / should be:

    string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
    IEnumerable<Book> books = files.Select(f => new Book(f, path));
    Author.SaveAllNew();
    Series.SaveAllNew();
    Book.SaveAllNew();

    The Book constructor determines the Series and Author and creates them as needed. So where is everybody? My DB is still empty... ...later. ...much later. ...until I remembered Linq methods use deferred execution, that is... :doh:

    string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
    IEnumerable<Book> books = files.Select(f => new Book(f, path)).ToList();
    Author.SaveAllNew();
    Series.SaveAllNew();
    Book.SaveAllNew();

    :sigh:

    Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #3

    Assuming you're using .NET 4.0 or higher, Directory.EnumerateFiles[^] is more efficient than Directory.GetFiles[^], as it doesn't have to allocate an array to contain every file path. Also, unless you specifically want to filter out files without extensions, use "*" instead of "*.*" as your search pattern. And since you don't seem to be using the result of the ToList call, it would probably be better to avoid creating a list. Unfortunately, there's no built-in Consume method, but it's not hard to write one:

    public static class NomNomNom
    {
    public static void Consume(this IEnumerable source)
    {
    if (source == null) throw new ArgumentNullException("source");
    foreach (T item in source);
    }
    }


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    OriginalGriffO A 2 Replies Last reply
    0
    • Richard DeemingR Richard Deeming

      Assuming you're using .NET 4.0 or higher, Directory.EnumerateFiles[^] is more efficient than Directory.GetFiles[^], as it doesn't have to allocate an array to contain every file path. Also, unless you specifically want to filter out files without extensions, use "*" instead of "*.*" as your search pattern. And since you don't seem to be using the result of the ToList call, it would probably be better to avoid creating a list. Unfortunately, there's no built-in Consume method, but it's not hard to write one:

      public static class NomNomNom
      {
      public static void Consume(this IEnumerable source)
      {
      if (source == null) throw new ArgumentNullException("source");
      foreach (T item in source);
      }
      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #4

      I use the list later on for UI updates - I just didn't show that part!

      Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Assuming you're using .NET 4.0 or higher, Directory.EnumerateFiles[^] is more efficient than Directory.GetFiles[^], as it doesn't have to allocate an array to contain every file path. Also, unless you specifically want to filter out files without extensions, use "*" instead of "*.*" as your search pattern. And since you don't seem to be using the result of the ToList call, it would probably be better to avoid creating a list. Unfortunately, there's no built-in Consume method, but it's not hard to write one:

        public static class NomNomNom
        {
        public static void Consume(this IEnumerable source)
        {
        if (source == null) throw new ArgumentNullException("source");
        foreach (T item in source);
        }
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        A Offline
        A Offline
        Argonia
        wrote on last edited by
        #5

        One does not simply lecture OG on programming . :laugh:

        Microsoft ... the only place where VARIANT_TRUE != true

        Richard DeemingR OriginalGriffO 2 Replies Last reply
        0
        • A Argonia

          One does not simply lecture OG on programming . :laugh:

          Microsoft ... the only place where VARIANT_TRUE != true

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #6

          Oh, come on! He describes himself as the CEO of Wales. When was the last time you met a CEO who knew the first thing about writing code? :rolleyes:


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          A 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Oh, come on! He describes himself as the CEO of Wales. When was the last time you met a CEO who knew the first thing about writing code? :rolleyes:


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            A Offline
            A Offline
            Argonia
            wrote on last edited by
            #7

            Richard Deeming wrote:

            When was the last time you met a CEO who knew the first thing about writing code?

            I couldn't know after all I work as a code monkey. I don't meet such people like CEOs. But your post was funny :D :thumbsup:

            Microsoft ... the only place where VARIANT_TRUE != true

            P 1 Reply Last reply
            0
            • A Argonia

              Richard Deeming wrote:

              When was the last time you met a CEO who knew the first thing about writing code?

              I couldn't know after all I work as a code monkey. I don't meet such people like CEOs. But your post was funny :D :thumbsup:

              Microsoft ... the only place where VARIANT_TRUE != true

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #8

              Argonia wrote:

              I don't meet such people like CEOs

              Undo another button or two.

              You'll never get very far if all you do is follow instructions.

              1 Reply Last reply
              0
              • A Argonia

                One does not simply lecture OG on programming . :laugh:

                Microsoft ... the only place where VARIANT_TRUE != true

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #9

                He can if he wants - I make mistakes like everyone else, and am both very aware and very thankful that I don't know everything. Mind you, I did know everything when I was 18... :laugh:

                Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                V 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  He can if he wants - I make mistakes like everyone else, and am both very aware and very thankful that I don't know everything. Mind you, I did know everything when I was 18... :laugh:

                  Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                  V Offline
                  V Offline
                  V 0
                  wrote on last edited by
                  #10

                  OriginalGriff wrote:

                  when I was 18...

                  That was surely before the dawn of men (and thus before the age of the personal computer) ;P

                  V.
                  (MQOTD rules and previous solutions)

                  OriginalGriffO 1 Reply Last reply
                  0
                  • V V 0

                    OriginalGriff wrote:

                    when I was 18...

                    That was surely before the dawn of men (and thus before the age of the personal computer) ;P

                    V.
                    (MQOTD rules and previous solutions)

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #11

                    Pre-PC indeed, but not Pre-DoM: I'm not 'Enry Minute!

                    Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    C 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      Pre-PC indeed, but not Pre-DoM: I'm not 'Enry Minute!

                      Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

                      C Offline
                      C Offline
                      CBadger
                      wrote on last edited by
                      #12

                      OriginalGriff wrote:

                      Pre-PC technology indeed,

                      FTFY :rolleyes:

                      »»» Loading Signature ««« · · · Please Wait · · ·    :badger:   :badger:   :badger:

                      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