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. Using linq to get data from a datatable

Using linq to get data from a datatable

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqtutorial
7 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.
  • M Offline
    M Offline
    Mycroft Holmes
    wrote on last edited by
    #1

    I have a standard requirement where I need to get an ID value from a datatable based on a string value from a description field. To date this is done either by a stored proc or using a filtered dataview or even a datatable.select. I would like to get it using Linq. So far most examples seem to return a query as ienumerable of datarows so they can be bound to controls. This seems to be the default MS example. I want the int value to be returned in one statement. Is this possible without recasting the result to a datatable/row to get the value.

    Never underestimate the power of human stupidity RAH

    A M 2 Replies Last reply
    0
    • M Mycroft Holmes

      I have a standard requirement where I need to get an ID value from a datatable based on a string value from a description field. To date this is done either by a stored proc or using a filtered dataview or even a datatable.select. I would like to get it using Linq. So far most examples seem to return a query as ienumerable of datarows so they can be bound to controls. This seems to be the default MS example. I want the int value to be returned in one statement. Is this possible without recasting the result to a datatable/row to get the value.

      Never underestimate the power of human stupidity RAH

      A Offline
      A Offline
      ABitSmart
      wrote on last edited by
      #2

      You can use First/FirstOrDefault to return a single value :) Example[^]

      M 1 Reply Last reply
      0
      • A ABitSmart

        You can use First/FirstOrDefault to return a single value :) Example[^]

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        I'm actually after his 2nd example except I want both the select and the subsequent test for an existing record in 1 query.

        iID Convert.ToInt32(data) = (from o in SupplierType
        where o.Description = sDescription
        select o.ID);

        I will then deal with the integer iID which will be 0 or a record ID value.

        Never underestimate the power of human stupidity RAH

        A 1 Reply Last reply
        0
        • M Mycroft Holmes

          I'm actually after his 2nd example except I want both the select and the subsequent test for an existing record in 1 query.

          iID Convert.ToInt32(data) = (from o in SupplierType
          where o.Description = sDescription
          select o.ID);

          I will then deal with the integer iID which will be 0 or a record ID value.

          Never underestimate the power of human stupidity RAH

          A Offline
          A Offline
          ABitSmart
          wrote on last edited by
          #4

          You could you FirstOrDefault which will return null if no value is present (or you can specify a default value).

          int[] numbers = { 1, 2, 3, 4, 5, 11 };
          // throws an exception
          int firstBigNumber = (from p in numbers where p > 10 select p).First();

          // returns 0
          int firstBigNumber = (from p in numbers where p > 10 select p).FirstOrDefault();

          // returns 111
          int firstBigNumber = (from p in numbers where p > 100 select p).DefaultIfEmpty(111).FirstOrDefault();

          Example[^] Does it serve your purpose ?

          M 1 Reply Last reply
          0
          • A ABitSmart

            You could you FirstOrDefault which will return null if no value is present (or you can specify a default value).

            int[] numbers = { 1, 2, 3, 4, 5, 11 };
            // throws an exception
            int firstBigNumber = (from p in numbers where p > 10 select p).First();

            // returns 0
            int firstBigNumber = (from p in numbers where p > 10 select p).FirstOrDefault();

            // returns 111
            int firstBigNumber = (from p in numbers where p > 100 select p).DefaultIfEmpty(111).FirstOrDefault();

            Example[^] Does it serve your purpose ?

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Thank you - I can massage the third example with no trouble at all - I really need to get a book on this stuff shortly :-O

            Never underestimate the power of human stupidity RAH

            A 1 Reply Last reply
            0
            • M Mycroft Holmes

              Thank you - I can massage the third example with no trouble at all - I really need to get a book on this stuff shortly :-O

              Never underestimate the power of human stupidity RAH

              A Offline
              A Offline
              ABitSmart
              wrote on last edited by
              #6

              Always Welcome :thumbsup:

              1 Reply Last reply
              0
              • M Mycroft Holmes

                I have a standard requirement where I need to get an ID value from a datatable based on a string value from a description field. To date this is done either by a stored proc or using a filtered dataview or even a datatable.select. I would like to get it using Linq. So far most examples seem to return a query as ienumerable of datarows so they can be bound to controls. This seems to be the default MS example. I want the int value to be returned in one statement. Is this possible without recasting the result to a datatable/row to get the value.

                Never underestimate the power of human stupidity RAH

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                With thanks to ABitSmart a solution was found.

                string sDescription = "Source system";

                int iTypeID = (from t in oTable.AsEnumerable()
                where t.Field("Description").ToLower() == sDescription.ToLower()
                select t.Field("TypeID")).FirstOrDefault();

                iTypeID has a value of 0 ot the matching ID

                Never underestimate the power of human stupidity RAH

                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